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

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

python pandas groupby:用于分組的刪除列

python pandas groupby:用于分組的刪除列

阿晨1998 2022-05-24 16:14:29
print(my_df)   A    B    C0  1   20  0.11  1   30  0.22  1   40  0.33  2  200  0.74  2  300  0.85  2  400  0.9that I group by column 'A'grouped = my_df.groupby('A')that I transform into a list:grouped.apply(pd.Series.tolist)A1    [[1.0, 20.0, 0.1], [1.0, 30.0, 0.2], [1.0, 40....2    [[2.0, 200.0, 0.7], [2.0, 300.0, 0.8], [2.0, 4...However, I would like to get rid of the now redundant column 'A' values, to haveA1    [[20.0, 0.1], [30.0, 0.2], [40....2    [[200.0, 0.7], [300.0, 0.8], [4...the grouped object contains all three columns:grouped.obj.columnsIndex(['A', 'B', 'C'], dtype='object')but I cannot drop any of them:grouped.drop('A')AttributeError: Cannot access callable attribute 'drop' of 'DataFrameGroupBy' objects, try using the 'apply' methodhow can I use drop with apply?grouped.apply(pd.DataFrame.drop( ??? Thank youEdit:More concretely I am doinggrouped.apply(pd.Series.tolist).tolist()with this I obtain[[[1.0, 20.0, 0.1], [1.0, 30.0, 0.2], [1.0, 40.0, 0.3]], [[2.0, 200.0, 0.7], [2.0, 300.0, 0.8], [2.0, 400.0, 0.9]]]but I would like to have[[[20.0, 0.1], [30.0, 0.2], [40.0, 0.3]], [[200.0, 0.7], [300.0, 0.8], [400.0, 0.9]]]
查看完整描述

1 回答

?
ITMISS

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

試試下面的代碼,希望對(duì)你有幫助:


df.set_index('A',inplace=True)

df.groupby('A').apply(pd.Series.tolist)

輸出將是:


A

1       [[20.0, 0.1], [30.0, 0.2], [40.0, 0.3]]

2    [[200.0, 0.7], [300.0, 0.8], [400.0, 0.9]]

dtype: object


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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