Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值应用于透视级别中的所有成员_Python_Pandas_Pivot Table_Pandas Groupby - Fatal编程技术网

Python 将值应用于透视级别中的所有成员

Python 将值应用于透视级别中的所有成员,python,pandas,pivot-table,pandas-groupby,Python,Pandas,Pivot Table,Pandas Groupby,我有一个简单的Pandas数据帧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 0000

我有一个简单的Pandas数据帧
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
> print t.groupby('group_id')['traitx'].all()

group_id
groupA    False
groupB     True
groupC     True
Name: traitx, dtype: bool
目标:我需要计算属于
组id
的行总数,该组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
然后只做一个
组跟踪的总和

我可以用以下公式计算
组跟踪

  > 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
> print t.groupby('group_id')['traitx'].all()

group_id
groupA    False
groupB     True
groupC     True
Name: traitx, dtype: bool
但是,我不知道如何将结果“涂抹”回原始数据帧中的
group\u traitx
列中

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

您可以使用:

不需要新列:

print (df.groupby(level=0)['traitx'].transform('all').sum())
3
如果只需要所有
True
组,请使用:

编辑:

如果在
组id
项目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(唯一对
组id
项目id
):
pivot\u table
使用默认聚合函数
mean
,但需要通过以下方式聚合:


太神了我必须进行分组(级别=1),但除此之外,这一切都非常有效。transform()的文档有点不透明。你们有什么指针可以让我看到要传递的可接受函数名列表,或者任何全面的例子吗?你们可以使用很多函数-请参阅。
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