这可能被认为是a thorough explanation of various approaches的副本,但是由于数据帧的数量较多,我似乎无法找到解决问题的方法。
我有多个数据帧(超过10个),每个数据帧在一列中不同。这只是一个简单快速的例子:
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']})
每个df
对于相同的剖面具有相同或不同的深度,因此
我需要创建一个新的数据帧,它将合并所有独立的数据帧,其中操作的键列是depth
和profile
,每个配置文件的都显示深度值。
因此,VARX
值应该是NaN
,其中没有该轮廓的该变量的深度测量。
结果应该是一个新的压缩数据帧,所有的VARX
都作为depth
和profile
列的附加列,如下所示:
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
注意,配置文件的实际数量要大得多。
有什么想法吗?
我会用append。
显然,如果您有很多数据帧,只需制作一个列表并循环遍历它们。
functools.partial
/reduce
一种方法是使用
partial
,正如docs中所提到的,它允许“冻结”函数的参数和/或关键字的某些部分,从而生成具有简化签名的新对象。然后使用reduce
我们可以累积地将新的部分对象应用于iterable的项(这里是数据帧列表):尽管我们总是可以使用注释中提到的正则
for
循环:考虑在每个数据帧上设置索引,然后使用
pd.concat
运行水平合并:相关问题 更多 >
编程相关推荐