【问题标题】:Merging Pandas DataFrames by column按列合并 Pandas DataFrame
【发布时间】:2020-11-02 18:57:18
【问题描述】:

我有两个数据框:

df1 = pd.DataFrame({'dateRep': ['2020-09-10', '2020-08-10', '2020-07-10', 
                              '2020-24-03', '2020-23-03', '2020-22-03'],
                    'cases': [271, 321, 137, 
                              8, 0, 1],
                    'countriesAndTerritories': ['Kenya', 'Kenya','Kenya',
                                               'Uganda', 'Uganda', 'Uganda']})
df1

df2 = pd.DataFrame({'date': ['2020-15-02', '2020-16-02', '2020-17-02', 
                              '2020-08-10', '2020-07-10', '2020-06-10'],
                    'cases': [2.0, 0.0, 5.0, 
                              -3.7, -5.0, 0.0],
                    'country_refion': ['Kenya', 'Kenya','Kenya', 
                                       'Uganda', 'Uganda', 'Uganda']})
df2

我想按日期组合或合并到一个数据框中:

df3 = pd.DataFrame({'date': ['2020-15-02', '2020-16-02', '2020-17-02',
                             '2020-22-03', '2020-23-03', '2020-24-03',
                             '2020-06-10', '2020-07-10', '2020-07-10', '2020-08-10', '2020-08-10', '2020-09-10'],
                    'cases': [2.0, 0.0, 5.0, 
                              1.0, 0.0, 8.0, 
                              0.0, -5.0, 137, -3.7, 321, 271],
                    'country_refion': ['Kenya', 'Kenya','Kenya',
                                       'Uganda', 'Uganda', 'Uganda',
                                       'Uganda', 'Uganda', 'Kenya', 'Uganda', 'Kenya', 'Kenya']})
df3

我尝试了 .join() 和 .concatenate() 方法,但没有按预期工作。提前感谢您的帮助!

【问题讨论】:

    标签: pandas dataframe join merge


    【解决方案1】:

    您可以将df.appenddf.combine_first() 一起使用:

    In [297]: x = df1.append(df2)
    In [299]: x.date = x.dateRep.combine_first(x.date)
    In [301]: x.country_refion = x.country_refion.combine_first(x.countriesAndTerritories)
    
    In [308]: x = x.sort_values('date').drop(['dateRep', 'countriesAndTerritories'], axis=1).reset_index(drop=True)
    
    In [310]: x
    Out[310]: 
        cases        date country_refion
    0     0.0  2020-06-10         Uganda
    1   137.0  2020-07-10          Kenya
    2    -5.0  2020-07-10         Uganda
    3   321.0  2020-08-10          Kenya
    4    -3.7  2020-08-10         Uganda
    5   271.0  2020-09-10          Kenya
    6     2.0  2020-15-02          Kenya
    7     0.0  2020-16-02          Kenya
    8     5.0  2020-17-02          Kenya
    9     1.0  2020-22-03         Uganda
    10    0.0  2020-23-03         Uganda
    11    8.0  2020-24-03         Uganda
    

    【讨论】:

    • 哇,速度超级快。十分感谢!您的回答非常直观。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2019-11-10
    • 2020-06-15
    • 2016-09-05
    • 2016-11-22
    相关资源
    最近更新 更多