2 回答

TA貢獻(xiàn)1858條經(jīng)驗 獲得超8個贊
首先檢查索引標(biāo)簽和列
fact.index
fact.columns
如果您需要將索引轉(zhuǎn)換為列,請使用:
利用:
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貢獻(xiàn)1848條經(jīng)驗 獲得超6個贊
需要添加“ as_index=True ”
例如:“count_in = df.groupby(['time_in','id'], as_index=True )['time_in'].count()”
添加回答
舉報