Python 我如何比较每组大熊猫分组后的计数?
我有一个EAV表加载到熊猫数据帧中。我使用group by来计算每个实体的每个字段的出现次数。我想比较价格和产品名称的计数,提取计数不相等的计数,并提取它们进行处理(本例中为a和c)。 当前,计数也显示在与标题不同的行上。任何帮助都将不胜感激Python 我如何比较每组大熊猫分组后的计数?,python,pandas,count,entity-attribute-value,pandas-groupby,Python,Pandas,Count,Entity Attribute Value,Pandas Groupby,我有一个EAV表加载到熊猫数据帧中。我使用group by来计算每个实体的每个字段的出现次数。我想比较价格和产品名称的计数,提取计数不相等的计数,并提取它们进行处理(本例中为a和c)。 当前,计数也显示在与标题不同的行上。任何帮助都将不胜感激 Count Entity Attribute a ProductName 10 Price 11 b ProductName
Count
Entity Attribute
a ProductName 10
Price 11
b ProductName 2
Price 2
c ProductName 3
Price 4
我认为第一步是将实体
转换为列
:
df1 = df['Count'].unstack(0)
print (df1)
Entity a b c
Attribute
Price 11 2 4
ProductName 10 2 3
然后选择列依据并比较行依据(!=
),最后一个筛选列依据布尔掩码
:
print (df1.loc['Price'].ne(df1.loc['ProductName']))
Entity
a True
b False
c True
dtype: bool
df2 = df1.loc[:, df1.loc['Price'].ne(df1.loc['ProductName'])]
print (df2)
Entity a c
Attribute
Price 11 4
ProductName 10 3
如果只需要列名,则类似:
cols = df1.columns[df1.loc['Price'].ne(df1.loc['ProductName'])].tolist()
print (cols)
['a', 'c']
我认为第一步是将实体
转换为列
:
df1 = df['Count'].unstack(0)
print (df1)
Entity a b c
Attribute
Price 11 2 4
ProductName 10 2 3
然后选择列依据并比较行依据(!=
),最后一个筛选列依据布尔掩码
:
print (df1.loc['Price'].ne(df1.loc['ProductName']))
Entity
a True
b False
c True
dtype: bool
df2 = df1.loc[:, df1.loc['Price'].ne(df1.loc['ProductName'])]
print (df2)
Entity a c
Attribute
Price 11 4
ProductName 10 3
如果只需要列名,则类似:
cols = df1.columns[df1.loc['Price'].ne(df1.loc['ProductName'])].tolist()
print (cols)
['a', 'c']