我搜索了這一點,但還是不能讓我的頭輪組,所以......熊貓分配值從組的一個成員所有其他成員
的數據(dataFrame
)是這個樣子( *
表示期望的輸出):
id parentid page_number is_critical_page page_number_of_critical* page_numbers_not_critical* 0 1 1 1 True 1 2,3,4,5 1 2 1 2 False 1 2,3,4,5 2 3 1 3 False 1 2,3,4,5 3 4 1 4 False 1 2,3,4,5 4 5 1 5 False 1 2,3,4,5 5 6 2 1 False 2 1,3 6 7 2 2 True 2 1,3 7 8 2 3 False 2 1,3 8 9 3 1 False -1 1 9 10 4 1 True 1 -1
欲:
組行乘
parentid
:dgroups=dataFrame.groupby('parentid')
應用任意操作,以基團:
def func(grp): grp['has_critical_page'] = grp['is_critical_page'].sum()>0 # simple operation ### Apply operation here to generate: ### ?? grp['page_number_of_critical*'] = ... ?? # is a scalar ### ?? grp['page_numbers_not_critical'] = ... ?? # is a list return grp dgroups.apply(func) print dgroups.describe()
的-1
的是N /正如 - 可能是一個NaN
,None
,-99
或任何其它特殊值。
我不知道是否使用apply
,transform
,filter
等,或是否申請(..)func
到dataFrame
或這些組的行。
試圖避免當然循環....謝謝!
PS積分爲如何在組內處理的is_critical_page
多命中......
PPS不知道如何格式化數據表... – jtlz2
感謝誰固定的表... – jtlz2