【问题标题】:how to concat between columns keeping sequence unchanged in 2 dataframes pandas如何在 2 个数据帧 pandas 中保持序列不变的列之间连接
【发布时间】:2020-01-09 09:23:30
【问题描述】:

我有 2 个数据框,我想按如下方式相互连接:

df1:

index    394               min     FIC-2000      398           min       FFC
0       Recycle Gas        min       20K20       Compressor    min       20k
1       TT                 date       kg/h       AT            date       ..
2       nan              2011-03-02   -20.7                    2011-03-02
                         08:00:00                              08:00:00
3       nan              2011-03-02   -27.5                      ...
                         08:00:10


df2:

index    Unnamed:0    0       1  ..     394         395   .....
 0        Service     Prop   Prop1      Recycle Gas  RecG

输出 df3 应该是这样的:

df3

index    Unnamed:0    0        ..     394                            395..   
0        Service     Prop       Recycle Gas                          RecG
1                               Recycle Gas       min     FIC-2000
2                                                 min       20K20
3                                       TT        date       kg/h
4                                      nan       2011-03-02   -20.7
                                                 08:00:00    
5                                      nan       2011-03-02   -27.5 
                                                 08:00:10

我已尝试使用此代码:

df3=pd.concat([df1,df2), axis=1)

但这只是连接索引 394,而 df1 的其余部分被附加到 df2 数据帧的末尾。 知道怎么做吗?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    只需更改为axis=0。 考虑一下:

    输入:

    >>> df
       col1  col2  col3
    0     1     4     2
    1     2     1     5
    2     3     6   319
    >>> df_1
       col4  col5  col6
    0     1     4    12
    1    32    12     3
    2     3     2   319
    >>> df_2
       col1  col3  col6
    0    12    14     2
    1     4   132     3
    2    23    22     9
    

    连接不匹配(每列名称)

    >>> pd.concat([df, df_1], axis=0)
       col1  col2   col3  col4  col5   col6
    0   1.0   4.0    2.0   NaN   NaN    NaN
    1   2.0   1.0    5.0   NaN   NaN    NaN
    2   3.0   6.0  319.0   NaN   NaN    NaN
    0   NaN   NaN    NaN   1.0   4.0   12.0
    1   NaN   NaN    NaN  32.0  12.0    3.0
    2   NaN   NaN    NaN   3.0   2.0  319.0
    

    连续匹配:

    >>> pd.concat([df, df_1, df_2], axis=0)
       col1  col2   col3  col4  col5   col6
    0   1.0   4.0    2.0   NaN   NaN    NaN
    1   2.0   1.0    5.0   NaN   NaN    NaN
    2   3.0   6.0  319.0   NaN   NaN    NaN
    0   NaN   NaN    NaN   1.0   4.0   12.0
    1   NaN   NaN    NaN  32.0  12.0    3.0
    2   NaN   NaN    NaN   3.0   2.0  319.0
    0  12.0   NaN   14.0   NaN   NaN    2.0
    1   4.0   NaN  132.0   NaN   NaN    3.0
    2  23.0   NaN   22.0   NaN   NaN    9.0
    

    concat匹配,填充NaN-s(类比可以填充None-s)

    >>> pd.concat([df, df_1, df_2], axis=0).fillna(0) #in case you wish to prettify it, maybe in case of strings do .fillna('')
       col1  col2   col3  col4  col5   col6
    0   1.0   4.0    2.0   0.0   0.0    0.0
    1   2.0   1.0    5.0   0.0   0.0    0.0
    2   3.0   6.0  319.0   0.0   0.0    0.0
    0   0.0   0.0    0.0   1.0   4.0   12.0
    1   0.0   0.0    0.0  32.0  12.0    3.0
    2   0.0   0.0    0.0   3.0   2.0  319.0
    0  12.0   0.0   14.0   0.0   0.0    2.0
    1   4.0   0.0  132.0   0.0   0.0    3.0
    2  23.0   0.0   22.0   0.0   0.0    9.0
    
    

    编辑 由下方评论部分中与 OP 的对话触发。

    所以你这样做:

    (1) 连接数据帧

    df3=pd.concat([df1,df2], axis=0)
    

    (2) 在它们上加入另一个数据框:

    df5=pd.merge(df3, df4[["FIC", "min"]], on="FIC", how="outer")
    

    (如果您认为相关,您可能需要考虑后缀字段) 参考https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

    【讨论】:

    • 感谢您的回答。我收到以下错误:ValueError:计划形状未对齐
    • 也许这个会有所帮助:stackoverflow.com/a/27412913/11610186
    • 并非如此。我试过df2=df2[df1.columns].apply( lambda x: ','.join(x.dropna().astype(str)),axis=1),但我得到了错误 KeyError: "['min', 'FIX.P2-FIC-200607.F_CV'] not in index"
    • @jayjay62 抱歉,但看不出这里有什么关系 - 我以为你想连接 2 个 pandas 数据帧
    • 是的,你是对的,但我没有想法,这只是一个尝试。
    猜你喜欢
    • 2021-03-17
    • 2019-09-16
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    相关资源
    最近更新 更多