python pandas dataframe 合并_Python pandas 合并两个或多个DataFrame的方法代码

这篇博客探讨了如何使用Python的pandas库进行数据合并。通过示例代码展示了使用concat、merge和append方法将多个DataFrame合并成一个完整的数据集。内容涵盖了如何处理缺失值,并确保数据按照特定列(如'profile'和'depth')正确对齐。
摘要由CSDN通过智能技术生成

示例代码:import pandas as pd

df1 = pd.DataFrame({'depth': [0.500000, 0.600000, 1.300000],

'VAR1': [38.196202, 38.198002, 38.200001],

'profile': ['profile_1', 'profile_1','profile_1']})

df2 = pd.DataFrame({'depth': [0.600000, 1.100000, 1.200000],

'VAR2': [0.20440, 0.20442, 0.20446],

'profile': ['profile_1', 'profile_1','profile_1']})

df3 = pd.DataFrame({'depth': [1.200000, 1.300000, 1.400000],

'VAR3': [15.1880, 15.1820, 15.1820],

'profile': ['profile_1', 'profile_1','profile_1']})

要实现输出结果:

name_profile depth VAR1 VAR2 VAR3

profile_1 0.500000 38.196202 NaN NaN

profile_1 0.600000 38.198002 0.20440 NaN

profile_1 1.100000 NaN 0.20442 NaN

profile_1 1.200000 NaN 0.20446 15.1880

profile_1 1.300000 38.200001 NaN 15.1820

profile_1 1.400000 NaN NaN 15.1820

1、使用concat合并dfs = [df.set_index(['profile', 'depth']) for df in [df1, df2, df3]]

print(pd.concat(dfs, axis=1).reset_index())

# profile depth VAR1 VAR2 VAR3

# 0 profile_1 0.5 38.198002 NaN NaN

# 1 profile_1 0.6 38.198002 0.20440 NaN

# 2 profile_1 1.1 NaN 0.20442 NaN

# 3 profile_1 1.2 NaN 0.20446 15.188

# 4 profile_1 1.3 38.200001 NaN 15.182

# 5 profile_1 1.4 NaN NaN 15.182

2、使用merge合并from functools import partial, reduce

dfs = [df1,df2,df3]

df_final = pd.DataFrame(columns=df1.columns)

for df in dfs:

df_final = df_final.merge(df, on=['depth','profile'], how='outer')

depth VAR1 profile VAR2 VAR3

0 0.6 38.198002 profile_1 0.20440 NaN

1 0.6 38.198002 profile_1 0.20440 NaN

2 1.3 38.200001 profile_1 NaN 15.182

3 1.1 NaN profile_1 0.20442 NaN

4 1.2 NaN profile_1 0.20446 15.188

5 1.4 NaN profile_1 NaN 15.182

3、使用append合并>>> df1.append(df2).append(df3).sort_values('depth')

VAR1 VAR2 VAR3 depth profile

0 38.196202 NaN NaN 0.5 profile_1

1 38.198002 NaN NaN 0.6 profile_1

0 NaN 0.20440 NaN 0.6 profile_1

1 NaN 0.20442 NaN 1.1 profile_1

2 NaN 0.20446 NaN 1.2 profile_1

0 NaN NaN 15.188 1.2 profile_1

2 38.200001 NaN NaN 1.3 profile_1

1 NaN NaN 15.182 1.3 profile_1

2 NaN NaN 15.182 1.4 profile_1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值