第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按多列分組時“TypeError:無法將 bool 轉(zhuǎn)換為 numpy.ndarray”

按多列分組時“TypeError:無法將 bool 轉(zhuǎn)換為 numpy.ndarray”

阿晨1998 2022-06-14 10:11:22
我想按兩列對數(shù)據(jù)框進行分組,以總結(jié)每家商店的平均月銷售額。數(shù)據(jù)(fact熊貓數(shù)據(jù)框):store_id    sku_id  date    quantity    city    city    category    month0   354 31253   2017-08-08  1   Paris   Paris   Shirt   81   354 31253   2017-08-19  1   Paris   Paris   Shirt   82   354 31258   2017-07-30  1   Paris   Paris   Shirt   73   354 277171  2017-09-28  1   Paris   Paris   Shirt   94   174 295953  2017-08-16  1   London  London  Shirt   8分組基于store_idormonth只能正常工作,但是當(dāng)我嘗試同時按store_idand分組時month,我得到:groupby_month = fact['quantity'].groupby(fact['store_id', 'month'])---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-169-a8cffb72ab7c> in <module>----> 1 groupby_month = fact['quantity'].groupby(fact['store_id', 'month'])      2       3 D:\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)   2925             if self.columns.nlevels > 1:   2926                 return self._getitem_multilevel(key)-> 2927             indexer = self.columns.get_loc(key)   2928             if is_integer(indexer):   2929                 indexer = [indexer]D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)   2655                                  'backfill or nearest lookups')   2656             try:-> 2657                 return self._engine.get_loc(key)   2658             except KeyError:   2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine._get_loc_duplicates()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine._maybe_get_bool_indexer()TypeError: Cannot convert bool to numpy.ndarray
查看完整描述

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  


查看完整回答
反對 回復(fù) 2022-06-14
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗 獲得超6個贊

需要添加“ as_index=True ”

例如:“count_in = df.groupby(['time_in','id'], as_index=True )['time_in'].count()”


查看完整回答
反對 回復(fù) 2022-06-14
  • 2 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號