2 回答

TA贡献1858条经验 获得超8个赞
首先检查索引标签和列
fact.index
fact.columns
如果您需要将索引转换为列,请使用:
利用:
fact.reset_index()
然后你可以使用:
fact.groupby(['store_id', 'month'])['quantity'].mean()
输出:
store_id month
174 8 1
354 7 1
8 1
9 1
Name: quantity, dtype: int64
或更好:
fact['mean']=fact.groupby(['store_id', 'month'])['quantity'].transform('mean')
print(fact)
store_id sku_id date quantity city city.1 category month \
0 354 31253 2017-08-08 1 Paris Paris Shirt 8
1 354 31253 2017-08-19 1 Paris Paris Shirt 8
2 354 31258 2017-07-30 1 Paris Paris Shirt 7
3 354 277171 2017-09-28 1 Paris Paris Shirt 9
4 174 295953 2017-08-16 1 London London Shirt 8
mean
0 1
1 1
2 1
3 1
4 1

TA贡献1848条经验 获得超6个赞
需要添加“ as_index=True ”
例如:“count_in = df.groupby(['time_in','id'], as_index=True )['time_in'].count()”
添加回答
举报