【问题标题】:python Pandas append multiple dataframe [closed]已解决的 python Pandas 追加多个数据帧 [关闭]
【发布时间】:2022-01-23 14:32:51
【问题描述】:

我已经阅读了很多关于我的问题的解决方案,但其中任何一个都对我有用。 我有一个像这样的第一个df:

col1 col2 col4
a1 b1 d1

第二个 df 喜欢:

col2 col3 col5
b2 c2 e2

我想要:

col1 col2 col3 col4 col5
a1 b1 d1
b2 c2 e2

我阅读的每个解决方案都不适合我。

解决方案: 我用过

df = pandas.concat([df1, df2])

我的错误是由于后期处理

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您应该能够通过 pd.concat 并使用 sort 参数来实现此目的。

    >>> df1 = pd.DataFrame.from_dict({'col1':['a1'],'col2':['b1'],'col4':['d1']})
    
    >>> df2 = pd.DataFrame.from_dict({'col2':['b2'],'col3':['c2'],'col5':['e2']})
    
    >>> pd.concat([df1, df2], sort=True)
      col1 col2 col3 col4 col5
    0   a1   b1  NaN   d1  NaN
    0  NaN   b2   c2  NaN   e2
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2021-09-26
      • 2014-10-20
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多