所以我有以下代码将给定的数据帧分组转换为列并计算每个组的大小。这将创建一个包含一列和两个值的 Multiindex 的 DataFrame。之后,我将结果转换为将第二个索引作为列索引,将第一个索引作为行索引。
import pandas as pd
from random import choice
products=['car', 'pen', 'kitchen', 'bed']
df=pd.DataFrame([{'product': choice(products), 'Changing': choice([True, False])} for _ in range(10000)])
df_grp=df.groupby(['product', 'Changing']).size().to_frame()
print(df_grp)
print('-'*50)
d=[]
for i in df_grp.reset_index()['product'].unique():
d.append({'product':i, 'not changing': df_grp.loc[(i,False)][0], 'changing': df_grp.loc[(i,True)][0]})
print(pd.DataFrame(d).set_index('product'))
结果如下
0
product Changing
bed False 1253
True 1269
car False 1244
True 1275
kitchen False 1236
True 1271
pen False 1206
True 1246
--------------------------------------------------
not changing changing
product
bed 1253 1269
car 1244 1275
kitchen 1236 1271
pen 1206 1246
这正是我想要实现的。但是,我的方法似乎有点复杂。有没有更优雅的方法来做到这一点?也许使用内置的熊猫函数或 lambda 函数?另外我的方法不能很好地概括,因为我总是假设第二个索引是一个布尔值。
答案 0 :(得分:2)
通过 unstack()
尝试:
df_grp=df.groupby(['product', 'Changing']).size().unstack()
最后使用 columns
属性:
df_grp.columns=[f'{"not "*col}{df_grp.columns.name}' for col in df_grp][::-1]
#as suggested by @Cyttorak #rename column without hard coding
#OR
df_grp.columns=['not changing','changing'] #you have to manually write the names
df_grp
的输出:
not changing changing
product
bed 1210 1226
car 1220 1272
kitchen 1238 1277
pen 1267 1290