将多个DataFrame与偶尔重叠组合在一起

问题描述 投票:1回答:1

我有多个子数据框架,我从CSV文件中读取,我想使用pandas将它们组合到一个大的DataFrame中。

我的问题是,单独的子DataFrame中的某些列显示重叠。如果他们这样做,则需要将值插入到最终DataFrame中的正确位置。

通常,所有子DataFrame都有一个ID列 - 所有这些DataFrame的所有set值的ID应该与最终的大DataFrame的ID列组合。

每个ID都有一个特定的CODE分配给它,它在所有子数据框架中是一致的,所以它可能总是被覆盖,因为值应该保持不变。

我已经尝试了每一个方式,mergejoinconcat甚至普通的旧循环和索引,索引列,没有,你的名字 - 但是,无济于事。

我想补充一点,一些方法创建带有后缀的新列 - 但我的目的是将重叠列中的所有值组合成一个列,这样就不是一个选项/

这是一些示例数据:

import pandas as pd
import numpy as np

np.random.seed(42)

df_1 = pd.DataFrame({
    'ID':[3,4,5,6],
    'CODE':[2,2,5,4],
    'M1':np.random.rand(4),
    'M2':np.random.rand(4)    
})

df_2 = pd.DataFrame({
    'ID':[8,9,10],
    'CODE':[7,2,4],
    'M1':np.random.rand(3),
    'M2':np.random.rand(3)    
})


df_3 = pd.DataFrame({
    'ID':[3,4,5,6],
    'CODE':[2,2,5,4],
    'M3':np.random.rand(4),
    'M4':np.random.rand(4)    
})

df_4 = pd.DataFrame({
    'ID':[8,9,10],
    'CODE':[7,2,4],
    'M3':np.random.rand(3),
    'M4':np.random.rand(3)    
})

df_5 = pd.DataFrame({
    'ID':[8,9,10],
    'CODE':[7,2,4],
    'M5':np.random.rand(3),
    'M6':np.random.rand(3)    
})

使用mergehow="outer"我能够合并df_1df_2df_3,结果就像我需要它一样。

ID  CODE    M1  M2  M3  M4
0   3   2   0.374540    0.156019    0.181825    0.431945
1   4   2   0.950714    0.155995    0.183405    0.291229
2   5   5   0.731994    0.058084    0.304242    0.611853
3   6   4   0.598658    0.866176    0.524756    0.139494
4   8   7   0.601115    0.969910    NaN         NaN
5   9   2   0.708073    0.832443    NaN         NaN
6   10  4   0.020584    0.212339    NaN         NaN

但是添加df_4,数据会被添加到下面而不是插入到正确的位置(所以在这种情况下不会有NaNs):

    ID  CODE      M1          M2          M3          M4
0   3   2   0.374540    0.156019    0.181825    0.431945
1   4   2   0.950714    0.155995    0.183405    0.291229
2   5   5   0.731994    0.058084    0.304242    0.611853
3   6   4   0.598658    0.866176    0.524756    0.139494
4   8   7   0.601115    0.969910    NaN         NaN
5   9   2   0.708073    0.832443    NaN         NaN
6   10  4   0.020584    0.212339    NaN         NaN
7   8   7   NaN         NaN        0.292145     0.785176
8   9   2   NaN         NaN        0.366362     0.199674
9   10  4   NaN         NaN        0.456070     0.514234

最后,在此示例中组合所有DataFrame应该会产生以下结果:

    ID  CODE      M1          M2          M3          M4     M5         M6
0   3   2   0.374540    0.156019    0.181825    0.431945    NaN         NaN
1   4   2   0.950714    0.155995    0.183405    0.291229    NaN         NaN
2   5   5   0.731994    0.058084    0.304242    0.611853    NaN         NaN
3   6   4   0.598658    0.866176    0.524756    0.139494    NaN         NaN
4   8   7   0.601115    0.969910    0.292145    0.785176    0.592414    0.170524
5   9   2   0.708073    0.832443    0.366362    0.199674    0.046450    0.065051
6   10  4   0.020584    0.212339    0.456070    0.514234    0.607544    0.948885
python pandas dataframe
1个回答
1
投票

合并具有相同ID和代码的数据帧并将它们连接起来。

pd.concat([df_1.merge(df_3, how = 'outer'),df_2.merge(df_4, how = 'outer').merge(df_5, how = 'outer')], sort = True)

    ID  CODE    M1      M2          M3          M4          M5          M6
0   3   2   0.374540    0.156019    0.181825    0.431945    NaN         NaN
1   4   2   0.950714    0.155995    0.183405    0.291229    NaN         NaN
2   5   5   0.731994    0.058084    0.304242    0.611853    NaN         NaN
3   6   4   0.598658    0.866176    0.524756    0.139494    NaN         NaN
4   8   7   0.601115    0.969910    0.292145    0.785176    0.592415    0.170524
5   9   2   0.708073    0.832443    0.366362    0.199674    0.046450    0.065052
6   10  4   0.020584    0.212339    0.456070    0.514234    0.607545    0.948886

使用groupby的另一个解决方案Concat轴0上的所有数据帧,ID上的groupby,CODE和first()返回第一个非NaN值。

dfs = [df_1, df_2, df_3, df_4, df_5]

pd.concat(dfs, sort = False).groupby(['CODE', 'ID']).first().sort_index(level = 1).reset_index()
© www.soinside.com 2019 - 2024. All rights reserved.