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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何分組兩列和單詞計(jì)數(shù)熊貓(或python)中的最后一列

如何分組兩列和單詞計(jì)數(shù)熊貓(或python)中的最后一列

動(dòng)漫人物 2021-09-02 19:41:23
1)例如,我有 3 列,如下所示 date      categories     contents   2018-01   fish_tank1     Goldfish Gombessa Goosefish Gopher rockfish    2018-01   fish_tank2     Grass carp Goosefish Grayling mullet shark   2018-02   fish_tank2     Goosefish Gopher rockfish Grayling mullet shark   2018-01   fish_tank1     carp Goosefish Grayling Goldfish Gombessa    2018-02   fish_tank2     carp Goosefish Grayling Grass carp Goosefish   2018-03   fish_tank3     Grass carp Goosefish Grayling mullet shark   2018-03   fish_tank2     Goosefish Gopher rockfish Goosefish Grayling  2)我有點(diǎn)想做df.groupby(['date','categories']).agg(df.contents.str.split(expand=True).stack().value_counts()得到類似下面的結(jié)果。但最近幾天我無(wú)法弄清楚這一點(diǎn)。    date   categories       contents   2018-01   fish_tank1  2    Goldfish    2                               Gombessa    2                               Goosefish   2                                Gopher      1                               rockfish    1                               ......              fish_tank2      Grass    1                              carp     1                              .....   2018-02   fish_tank2     Goosefish    3                            Grayling     2                            Gopher       1                            ........    ........................  3)誰(shuí)能給我洞察力來(lái)獲得我想做的結(jié)果?
查看完整描述

1 回答

?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊

用 -


from collections import Counter

df['contents2'] = df['contents'].str.split()

df.groupby(['date', 'categories'])['contents2'].apply(lambda x: Counter(x.sum()))

輸出


date     categories           

2018-01  fish_tank1  Goldfish     2.0

                     Gombessa     2.0

                     Goosefish    2.0

                     Gopher       1.0

                     Grayling     1.0

                     carp         1.0

                     rockfish     1.0

         fish_tank2  Goosefish    1.0

                     Grass        1.0

                     Grayling     1.0

                     carp         1.0

                     mullet       1.0

                     shark        1.0

2018-02  fish_tank2  Goosefish    3.0

                     Gopher       1.0

                     Grass        1.0

                     Grayling     2.0

                     carp         2.0

                     mullet       1.0

                     rockfish     1.0

                     shark        1.0

2018-03  fish_tank2  Goosefish    2.0

                     Gopher       1.0

                     Grayling     1.0

                     rockfish     1.0

         fish_tank3  Goosefish    1.0

                     Grass        1.0

                     Grayling     1.0

                     carp         1.0

                     mullet       1.0

                     shark        1.0

Name: contents2, dtype: float64


查看完整回答
反對(duì) 回復(fù) 2021-09-02
  • 1 回答
  • 0 關(guān)注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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