2
我有這個問題是在閱讀"Python pandas groupby object apply method duplicates first group"後的擴展。Python熊貓羣體對象申請方法增加索引
我得到了答案,並試圖對我自己的一些實驗,例如:
import pandas as pd
from cStringIO import StringIO
s = '''c1 c2 c3
1 2 3
4 5 6'''
df = pd.read_csv(StringIO(s), sep=' ')
print df
def f2(df):
print df.iloc[:]
print "--------"
return df.iloc[:]
df2 = df.groupby(['c1']).apply(f2)
print "======"
print df2
給出預期:
c1 c2 c3
0 1 2 3
1 4 5 6
c1 c2 c3
0 1 2 3
--------
c1 c2 c3
0 1 2 3
--------
c1 c2 c3
1 4 5 6
--------
======
c1 c2 c3
0 1 2 3
1 4 5 6
然而,當我嘗試只返回df.iloc [0 ]:
def f3(df):
print df.iloc[0:]
print "--------"
return df.iloc[0:]
df3 = df.groupby(['c1']).apply(f3)
print "======"
print df3
,我得到一個額外的指標:
c1 c2 c3
0 1 2 3
--------
c1 c2 c3
0 1 2 3
--------
c1 c2 c3
1 4 5 6
--------
======
c1 c2 c3
c1
1 0 1 2 3
4 1 4 5 6
我做了一些搜索和懷疑這可能意味着有不同的代碼路徑?
似乎完全正確(也嘗試過= x.iloc [0:1]; out.name = x.name,並得到額外的索引)。另外,Scikit-Learn上的酷視頻,你搖滾:) – ntg
也試過= x.iloc [0:1]; out.name = x.name,並且獲得了額外的索引,但前提是返回的結果在存在重複的c1值時會有所不同。 – ntg