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

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

根據(jù)多個條件提取行。包含數(shù)據(jù)示例

根據(jù)多個條件提取行。包含數(shù)據(jù)示例

繁星淼淼 2023-10-11 15:57:25
我正在嘗試根據(jù)條件從 dataframe1 到 dataframe2 提取行,但我很掙扎,有人可以幫助我嗎?那就太棒了。數(shù)據(jù)框1:df1 = pd.DataFrame([[1001, 'democrat',0.23],[1001, 'republican',0.7],[1001, 'others',0.07],[1003, 'democrat',0.33],[1003, 'republican',0.44],[1003, 'others',0.23]], columns=['Fips_code', 'Partisan', 'Vote_Pct'])數(shù)據(jù)框2:df2 = pd.DataFrame([[1001],[1003], [1005]], columns=['Fips_code'])我想根據(jù) fips 代碼條件將三列添加到 dataframe2 中,如下所示 ('democrat_vote_pct','republican_vote_pct','others_vote_pct')。期望出:df2 = pd.DataFrame([[1001,0.23,0.7,0.07],[1003,0.33,0.44,0.23], [1005, NA, NA, NA]], columns=['Fips_code','democrat_vote_pct','republican_vote_pct','others_vote_pct'])請幫我..
查看完整描述

1 回答

?
HUX布斯

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

你可以嘗試這樣的事情:


_df2 = df1.pivot_table(values='Vote_Pct', index='Fips_code', columns='Partisan')

進而:


result = df2.merge(_df2, on='Fips_code', how='left')

這給了你你想要的:


   Fips_code  democrat  others  republican

0       1001      0.23    0.07        0.70

1       1003      0.33    0.23        0.44

2       1005       NaN     NaN         NaN

如果您希望列具有不同的名稱,只需給它一個.rename()


new_cols = {'democrat': 'democrat_pct_votes',

            'others': 'others_pct_votes',

            'republican': 'republican_pct_votes'}

df2.rename(new_cols, axis=1, inplace=True)


查看完整回答
反對 回復 2023-10-11
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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