在一行中:
pd.concat([
pd.concat((pd.Series(pd.date_range(start=f'{y}/{m}', end=pd.Timestamp(f'{y}/{m}') + pd.offsets.MonthEnd(0), freq='D'), name='Day'),
df[c].rename('mean_temp')[:pd.Period(f'{y}/{m}').days_in_month]), axis=1)
for y, df in sorted(temp_dfs.items())
for m, c in enumerate(['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'], start=1)
], axis=0)
结果如下:
Day mean_temp
0 2015-01-01 16
1 2015-01-02 29
2 2015-01-03 33
3 2015-01-04 28
4 2015-01-05 17
.. ... ...
26 2019-12-27 32
27 2019-12-28 39
28 2019-12-29 -2
29 2019-12-30 39
30 2019-12-31 1
前面的代码假设temp_dfs
a 是按年份组织的dict
所有DataFrame
s :键是所有可用的年份。我使用此代码生成示例dict
:
import pandas as pd
import numpy as np
temp_dfs = {
y: pd.DataFrame(
data=np.column_stack((np.arange(1, 32), np.random.randint(-3, 40, (31, 12)))),
columns=['Day', 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
) for y in range(2015, 2020)
}