Python 合并数据帧:保留行冗余,同时删除列冗余

Python 合并数据帧:保留行冗余,同时删除列冗余,python,pandas,Python,Pandas,我有三个数据帧: df1: col1 col2 col3 name1 human experID1 name2 mouse experID2 name3 human experID3 name4 mouse experID4 name5 human experID5 df2: col1 col2 col4 col6 name1 human experID1 output1 name2 human exp

我有三个数据帧:

df1:
col1    col2    col3
name1   human   experID1
name2   mouse   experID2
name3   human   experID3
name4   mouse   experID4
name5   human   experID5

df2:
col1    col2    col4    col6
name1   human   experID1    output1
name2   human   experID2    output2
name3   human   experID3    output3
name10  human   experID10   output4

df3:
col1    col3    col7    col8
name1   happy   human   ref1
name2   sad mouse   ref2
name3   angry   human   ref3
我想把它们结合起来:

  • 列1中的行必须保持不变,即,由于名称1出现在col1中的每个数据帧中,因此它应该在最终数据帧中出现三次

  • 我只想组合这些列,以便:如果该列已经存在,则将数据添加到该列;否则,添加一个新列

  • 在缺少的单元格中填入“-”

  • 因此,输出将是:

    col1    col2    col3    col4    col6    col7    col8
    name1   human   experID1    -   -   -   -
    name2   mouse   experID2    -   -   -   -
    name3   human   experID3    -   -   -   -
    name4   mouse   experID4    -   -   -   -
    name5   human   experID5    -   -   -   -
    name1   human   -   experID1    output1 -   -
    name2   human   -   experID2    output2 -   -
    name3   human   -   experID3    output3 -   -
    name10  human   -   experID10   output4 -   -
    name1   -   happy   -   -   human   ref1
    name2   -   sad -   -   mouse   ref2
    name3   -   angry   -   -   human   ref3
    
    在展示我所尝试的方面: 我有三个数据帧,df1,df2,df3

    正在尝试使用merge、concat和append,例如:

    final_df = pd.DataFrame()
    list_of_df = [df1,df2,df3]
    
    #method 1
    result = pd.concat(list_of_df)
    
    #method 2
    for each_df in list_of_dfs:
    #this is where it started to go wrong
    
    我尝试了页面上的所有方法,但我认为它们并没有达到我想要的效果(如果您想让我在这里添加此页面的代码,请告诉我,我只是认为,既然它是错误的,指向链接会更整洁)


    从逻辑上讲,我想我希望将每个数据帧逐个“附加”到主数据帧,以便行保持不变。如果有人能举个例子的话,我不理解的只是合并列而不合并行。

    看起来,你只想添加

    df1.append(df2, sort=False).append(df3, sort=False).fillna('-')
    

    看起来,你只是想附加

    df1.append(df2, sort=False).append(df3, sort=False).fillna('-')
    
    使用:


    使用:



    您需要sort=False,这样警告就不会出现,cocnat更快您能证明
    concat
    更快吗@ansev因为它也是一个
    循环的
    实现我已经测量过了,你自己测量过了,如果你想添加n个数据帧,你必须使用append(n-1)次?这是没有意义的…@erfanny如果你需要sort=False,这样警告就不会出现,cocnat就快了你能证明
    concat
    更快吗@ansev因为它也是一个
    循环的
    实现我已经测量过了,你自己测量过了,如果你想添加n个数据帧,你必须使用append(n-1)次?那没有道理……@Erfan
    
         col1   col2      col3       col4     col6   col7  col8
    0   name1  human  experID1          -        -      -     -
    1   name2  mouse  experID2          -        -      -     -
    2   name3  human  experID3          -        -      -     -
    3   name4  mouse  experID4          -        -      -     -
    4   name5  human  experID5          -        -      -     -
    0   name1  human         -   experID1  output1      -     -
    1   name2  human         -   experID2  output2      -     -
    2   name3  human         -   experID3  output3      -     -
    3  name10  human         -  experID10  output4      -     -
    0   name1      -     happy          -        -  human  ref1
    1   name2      -       sad          -        -  mouse  ref2
    2   name3      -     angry          -        -  human  ref3