需要合并2个数据帧

2024-10-01 15:40:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要合并2个数据帧:

Siren libelleVoieEtablissement
0     one
1     two
2     three

Siren denominationUniteLegale 
0     A
1     B
3     D
4     E

我需要什么:

Siren denominationUniteLegale libelleVoieEtablissement
0     A                       one
1     B                       two
2     NaN                     three

我得到的:

Siren denominationUniteLegale libelleVoieEtablissement
0     A                       one
1     B                       two
2     NaN                     three
3     D                       NaN
4     E                       NaN

我试过内部和外部的,但对这个案子没用

df1 = pd.merge(df1, df2, on=['key'], how='left')

我不想要键为3的行,怎么办?我把融合的“方式”改为内在的、外在的和正确的,但没有比这更好的


Tags: 数据keyonmergenanonesirenpd
1条回答
网友
1楼 · 发布于 2024-10-01 15:40:39

我想join()就是你要找的

a = {'street':['one','two','three']}
b = {'name':['A','B','C','D']}

a = pd.DataFrame(a)
b = pd.DataFrame(b)
c = a.join(b)
c = c[['name','street']]
print(c)

输出:

  name street
0    A    one
1    B    two
2    C  three

相关问题 更多 >

    热门问题