python - 组合多个偶尔重叠的 DataFrame

标签 python pandas dataframe

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

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

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

每个ID都分配有一个特定的CODE,该代码在所有子DataFrame中都是一致的,因此它可能总是被覆盖,因为值应该保持不变.

我尝试了各种方法,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 后,数据会附加到下面,而不是插入到正确的位置(因此在这种情况下不会出现 NaN):

    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

最佳答案

合并具有相同 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 的另一个解决方案。连接轴 0 上的所有数据帧,对 ID、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()

关于python - 组合多个偶尔重叠的 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426620/

相关文章:

python - tkinter - 设置几何而不显示窗口

python - 提高挂载目录的 IO 性能?

python - 确认 import * 和 from xxx import * 的区别

python - 将数据从 JSON 转换为 pandas 数据框

python - Pandas groupby 箱线图上的轴错误

python - 将多索引数据帧写入 csv 而不更改其格式

Python 从 excel 数据创建字典

python - Pandas:带有给定的一组 y 轴值类别图的标签

Python Pandas : Breaking a list or series into columns of different sizes

python - 如何将嵌套的 Struct 列展开为多列?