将值应用于pi中的所有成员

2024-10-03 23:28:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的熊猫数据帧t,如下所示:

  > print t

    group_id    item_id  traitx
  0   groupA  000001-00    True
  1   groupA  000002-00    True
  2   groupA  000003-00   False
  3   groupB  000001-00    True
  4   groupC  000002-00    True
  5   groupC  000004-00    True

  > t.pivot_table(index=['groupid', 'item_id'])

                      traitx
  group_id item_id          
  groupA   000001-00    True
           000002-00    True
           000003-00   False
  groupB   000001-00    True
  groupC   000001-00    True
           000002-00    True

目标:我需要计算属于group_id的行的总数,这些行的traitx值都是True。你知道吗

我解决这个问题的想法是以某种方式添加一个列,它将显示整个组对于每一行是否是True,例如

    group_id    item_id  traitx  group_traitx
  0   groupA  000001-00    True         False
  1   groupA  000002-00    True         False
  2   groupA  000003-00   False         False
  3   groupB  000001-00    True         True
  4   groupC  000002-00    True         True
  5   groupC  000004-00    True         True

然后求group_traitx的和。你知道吗

我可以用以下公式计算group_traitx

> print t.groupby('group_id')['traitx'].all()

group_id
groupA    False
groupB     True
groupC     True
Name: traitx, dtype: bool

但是,我不知道如何将结果“涂抹”回原始数据帧中的group_traitx列。你知道吗

免责声明-我昨天刚开始使用熊猫,所以这可能不是实现我最初目标的最佳方式。你知道吗


Tags: 数据idfalsetrue目标方式tablegroup
1条回答
网友
1楼 · 发布于 2024-10-03 23:28:02

您可以使用^{}

df= t.pivot_table(index=['group_id', 'item_id'])
df['group_traitx'] = df.groupby(level=0)['traitx'].transform('all')
print (df)
                    traitx  group_traitx
group_id item_id                        
groupA   000001-00    True         False
         000002-00    True         False
         000003-00   False         False
groupB   000001-00    True          True
groupC   000002-00    True          True
         000004-00    True          True

print (df['group_traitx'].sum())
3

不需要新列:

print (df.groupby(level=0)['traitx'].transform('all').sum())
3

如果只需要所有True组,则使用filter

df= t.pivot_table(index=['group_id', 'item_id'])
print (df.groupby(level=0)['traitx'].filter('all'))

group_id  item_id  
groupB    000001-00    True
groupC    000002-00    True
          000004-00    True
Name: traitx, dtype: bool

print (df.groupby(level=0)['traitx'].filter('all').sum())
3

编辑:

如果在group_iditem_id对中重复:

#added duplicates
print (t)
  group_id    item_id  traitx
0   groupA  000001-00    True
1   groupA  000001-00    True
2   groupA  000001-00   False
3   groupB  000001-00    True
4   groupC  000002-00    True
5   groupC  000004-00    True

#pivot_table is not necessary for new column of original df
t['group_traitx'] = t.groupby(['group_id', 'item_id'])['traitx'].transform('all')
print (t)
  group_id    item_id  traitx  group_traitx
0   groupA  000001-00    True         False
1   groupA  000001-00    True         False
2   groupA  000001-00   False         False
3   groupB  000001-00    True          True
4   groupC  000002-00    True          True
5   groupC  000004-00    True          True

如果需要使用聚合df(唯一对group_iditem_id): pivot_table使用默认聚合函数mean,但需要按^{}聚合:

print (t.pivot_table(index=['group_id', 'item_id']))
                      traitx
group_id item_id            
groupA   000001-00  0.666667
groupB   000001-00  1.000000
groupC   000002-00  1.000000
         000004-00  1.000000

df = t.pivot_table(index=['group_id', 'item_id'], aggfunc='all')
df['group_traitx'] = df.groupby(level=0)['traitx'].transform('all')
print (df)
                    traitx  group_traitx
group_id item_id                        
groupA   000001-00   False         False
groupB   000001-00    True          True
groupC   000002-00    True          True
         000004-00    True          True

相关问题 更多 >