<p>使用<code>list comprehension</code>和<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html" rel="nofollow">^{<cd2>}</a>的解决方案,最后将<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html" rel="nofollow">^{<cd3>}</a>所有数据放在一起:</p>
<pre><code>dates = [col for col in df.columns if 'Date' in col]
logs = [col for col in df.columns if 'Log' in col]
print ([df[[col[0], col[1]]].set_index(col[0], drop=False)
.reindex(df.Date1) for col in zip(dates, logs)])
[ Date1 Log1
Date1
01.01.2000 01.01.2000 1000
02.01.2000 02.01.2000 1050
03.01.2000 03.01.2000 1100, Date2 Log2
Date1
01.01.2000 NaN NaN
02.01.2000 02.01.2000 2000.0
03.01.2000 03.01.2000 1950.0, Date3 Log3
Date1
01.01.2000 01.01.2000 3000
02.01.2000 02.01.2000 3020
03.01.2000 03.01.2000 3000]
df1 = pd.concat([df[[col[0], col[1]]]
.set_index(col[0], drop=False)
.reindex(df.Date1) for col in zip(dates, logs)], axis=1)
df1.reset_index(inplace=True, drop=True)
print (df1)
Date1 Log1 Date2 Log2 Date3 Log3
0 01.01.2000 1000 NaN NaN 01.01.2000 3000
1 02.01.2000 1050 02.01.2000 2000.0 02.01.2000 3020
2 03.01.2000 1100 03.01.2000 1950.0 03.01.2000 3000
</code></pre>