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

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

合并多個(gè)數(shù)據(jù)框熊貓

合并多個(gè)數(shù)據(jù)框熊貓

手掌心 2021-03-30 13:14:36
我嘗試將多個(gè)新的dataFrames合并到一個(gè)主框架中。假設(shè)主數(shù)據(jù)框:      key1           key20   0.365803    0.2591121   0.086869    0.5898342   0.269619    0.1836443   0.755826    0.0451874   0.204009    0.669371我嘗試將以下兩個(gè)數(shù)據(jù)集合并到主要數(shù)據(jù)集“新數(shù)據(jù)1 ”中:        key1    key2    new feature0   0.365803    0.259112    info1新數(shù)據(jù)2:        key1    key2    new feature0   0.204009    0.669371    info2預(yù)期結(jié)果:       key1       key2  new feature0   0.365803    0.259112    info11   0.776945    0.780978    NaN2   0.275891    0.114998    NaN3   0.667057    0.373029    NaN4   0.204009    0.669371    info2我試過(guò)的test = test.merge(data1, left_on=['key1', 'key2'], right_on=['key1', 'key2'], how='left')test = test.merge(data2, left_on=['key1', 'key2'], right_on=['key1', 'key2'], how='left')第一個(gè)效果很好,但第二個(gè)效果不好,我得到的結(jié)果是:        key1    key2    new feature_x   new feature_y0   0.365803    0.259112    info1      NaN1   0.776945    0.780978    NaN        NaN2   0.275891    0.114998    NaN        NaN3   0.667057    0.373029    NaN        NaN4   0.204009    0.669371    NaN       info2謝謝你的幫助!
查看完整描述

3 回答

?
aluckdog

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

首先append或concat兩者DataFrame在一起,然后merge:


dat = pd.concat([data1, data2], ignore_index=True)

或者:


dat = data1.append(data2, ignore_index=True)


print (dat)

       key1      key2 new feature

0  0.365803  0.259112       info1

1  0.204009  0.669371       info2

#if same joined columns names better is only on parameter

df = test.merge(dat, on=['key1', 'key2'], how='left')


print (df)

       key1      key2 new feature

0  0.365803  0.259112       info1

1  0.086869  0.589834         NaN

2  0.269619  0.183644         NaN

3  0.755826  0.045187         NaN

4  0.204009  0.669371       info2


查看完整回答
反對(duì) 回復(fù) 2021-04-20
?
胡說(shuō)叔叔

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

您可以pd.DataFrame.update改用:


# create new column and set index

res = test.assign(newfeature=None).set_index(['key1', 'key2'])


# update with new data sequentially

res.update(data1.set_index(['key1', 'key2']))

res.update(data2.set_index(['key1', 'key2']))


# reset index to recover columns

res = res.reset_index()


print(res)


       key1      key2 newfeature

0  0.365803  0.259112      info1

1  0.086869  0.589834       None

2  0.269619  0.183644       None

3  0.755826  0.045187       None

4  0.204009  0.669371      info2


查看完整回答
反對(duì) 回復(fù) 2021-04-20
?
哈士奇WWW

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

您還可以將數(shù)據(jù)幀設(shè)置為相同的索引并使用簡(jiǎn)單 loc


df  = df.set_index(["key1", "key2"])

df2 = df2.set_index(["key1", "key2"])

然后


df.loc[:, "new_feature"] = df2['new_feature']


查看完整回答
反對(duì) 回復(fù) 2021-04-20
  • 3 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(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)