【问题标题】:Conecting DataFrame in pandas by column name [duplicate]按列名连接熊猫中的DataFrame [重复]
【发布时间】:2022-01-09 22:48:51
【问题描述】:

我有 3 个数据框:

第一:

Country col1
Afghanistan 6.4
Albania 7.3

秒:

Country col2
Afghanistan 610
Algeria 983

最后:

Country col3
Afghanistan 1
Angola 2

我想通过 col "Country" 连接它们并用 np.nan 填充空值 得到:

Country col1 col2 col3
Afghanistan 6.4 601 1
Albania 7.3 NaN NaN
Algeria NaN 983 NaN
Angola NaN NaN 2

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

您可以使用 df.merge() 和类型为外部。

import pandas as pd
from functools import reduce

#create dataframes
df1= pd.DataFrame([["Afghanistan", 6.4], ["Albania", 7.4]], columns=['Country', 'col1'])
df2= pd.DataFrame([["Afghanistan", 610], ["Algeria", 983]], columns=['Country', 'col2'])
df3= pd.DataFrame([["Afghanistan", 1], ["Angola", 2]], columns=['Country', 'col3'])

#merge them all on Country
df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['Country'],
                                            how='outer'), [df1, df2, df3])

       Country  col1   col2  col3
0  Afghanistan   6.4  610.0   1.0
1      Albania   7.4    NaN   NaN
2      Algeria   NaN  983.0   NaN
3       Angola   NaN    NaN   2.0

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 2017-10-01
    • 1970-01-01
    • 2018-07-11
    • 2023-04-10
    • 2018-08-26
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多