回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>这可能被认为是<a href="https://stackoverflow.com/questions/53645882/pandas-merging-101">a thorough explanation of various approaches</a>的副本,但是由于数据帧的数量较多,我似乎无法找到解决问题的方法。</p>
<p>我有<strong>多个数据帧(超过10个),每个数据帧在一列中不同。这只是一个简单快速的例子:</p>
<pre><code>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']})
</code></pre>
<p>每个<code>df</code>对于相同的剖面具有相同或不同的深度,因此</p>
<p>我需要创建一个新的数据帧,它将合并所有独立的数据帧,其中操作的<strong>键列是<code>depth</code>和<code>profile</code>,每个配置文件的<strong>都显示深度值。</p>
<p>因此,<code>VARX</code>值应该是<code>NaN</code>,其中没有该轮廓的该变量的深度测量。</p>
<p>结果应该是一个新的压缩数据帧,所有的<code>VARX</code>都作为<code>depth</code>和<code>profile</code>列的附加列,如下所示:</p>
<pre><code>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
</code></pre>
<p>注意,配置文件的实际数量要大得多。</p>
<p>有什么想法吗?</p>