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

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

根據(jù)python中列的標簽創(chuàng)建groupby

根據(jù)python中列的標簽創(chuàng)建groupby

海綿寶寶撒 2023-06-02 16:06:49
我有一個類似于這樣的大型數(shù)據(jù)框:id        price             status1           23               none2           23               none3           34               none4           32               none5           31               none6           37               none7           20               none8           29               none9           21               none10          22               done我想根據(jù)狀態(tài)進行分組。我想在這樣的情況下建立一個組:每次狀態(tài)完成時,它將成為一個組。到目前為止,我所做的是根據(jù)索引創(chuàng)建一個組:grouper = df.groupby(df.index // 10)但后來我意識到狀態(tài)是隨機寫入的,并不總是每 10 行寫入一次。我怎樣才能在 python 中制作它?謝謝
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

done通過 back by 的累積和比較值和 cretae 組iloc[::-1],最后添加另一個iloc[::-1]列的原始順序:


g = df['status'].eq('done').iloc[::-1].cumsum().iloc[::-1]

grouper = df.groupby(g, sort=False)

樣本:


#chnaged data for more groups   

print (df)

   id  price status

0   1     23   none

1   2     23   done

2   3     34   none

3   4     32   none

4   5     31   done

5   6     37   none

6   7     20   none

7   8     29   none

8   9     21   none

9  10     22   done


g = df['status'].eq('done').iloc[::-1].cumsum().iloc[::-1]

print (g)

0    3

1    3

2    2

3    2

4    2

5    1

6    1

7    1

8    1

9    1

Name: status, dtype: int32

grouper = df.groupby(g, sort=False)


for name, df in grouper:

    print (df)


   id  price status

0   1     23   none

1   2     23   done

   id  price status

2   3     34   none

3   4     32   none

4   5     31   done

   id  price status

5   6     37   none

6   7     20   none

7   8     29   none

8   9     21   none

9  10     22   done


查看完整回答
反對 回復(fù) 2023-06-02
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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