Python:旋转数据帧的一些(31天)列,并将它们与现有(年、月)行(NOAA数据)相匹配

Python:旋转数据帧的一些(31天)列,并将它们与现有(年、月)行(NOAA数据)相匹配,python,pandas,Python,Pandas,我有NOAA的气象数据。在原始状态下,它将年和月作为行,然后将天作为列。我想扩展行数,以便每行都有一年、一个月和一天,每行中都有相应的数据 还有一个天气变量列,其中每行表示每月收集的不同天气变量。一个月内收集的天气变量数量可能会发生变化。(1月份有两个(tmax,tmin),2月份有三个(tmax,tmin,prcp),3月份有一个(tmin) 下面是一个示例df example_df = pd.DataFrame({'station': ['USC1', 'USC1', 'USC1', 'US

我有NOAA的气象数据。在原始状态下,它将年和月作为行,然后将天作为列。我想扩展行数,以便每行都有一年、一个月和一天,每行中都有相应的数据

还有一个天气变量列,其中每行表示每月收集的不同天气变量。一个月内收集的天气变量数量可能会发生变化。(1月份有两个(tmax,tmin),2月份有三个(tmax,tmin,prcp),3月份有一个(tmin)

下面是一个示例df

example_df = pd.DataFrame({'station': ['USC1', 'USC1', 'USC1', 'USC1', 'USC1', 'USC1'],
           'year': [1993, 1993, 1993, 1993,1993, 1993],
           'month': [1, 1,  2, 2, 2, 3],
           'attribute':['tmax', 'tmin', 'tmax', 'tmin', 'prcp', 'tmax'],
           'day1': range(1, 7, 1),
           'day2': range(1, 7, 1),
           'day3': range(1, 7, 1),
           'day4': range(1, 7, 1),
                  })
example_df = example_df[['station', 'year', 'month', 'attribute', 'day1', 'day2', 'day3', 'day4']]
这就是我想要的解决方案

solution_df = pd.DataFrame({'station': ['USC1', 'USC1', 'USC1', 'USC1', 'USC1', 'USC1','USC1', 'USC1', 'USC1', 'USC1', 'USC1', 'USC1'],
           'year': [1993, 1993, 1993, 1993,1993, 1993, 1993, 1993, 1993, 1993,1993, 1993],
           'month': [1, 1,1, 1, 2, 2,  2, 2, 3, 3, 3, 3],
           'day':[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4],
           'tmax': [1, 1, 1, 1, 3, 3, 3, 3, 6, 6, 6, 6],
           'tmin': [2, 2, 2, 2, 4, 4, 4, 4, np.nan, np.nan, np.nan, np.nan],
           'prcp': [np.nan, np.nan, np.nan, np.nan, 5, 5, 5, 5, np.nan, np.nan, np.nan, np.nan]

                  })
solution_df = solution_df[['station', 'year', 'month', 'day', 'tmax', 'tmin', 'prcp']]
我尝试了.T、pivot、melt、stack和unstack,以使day列成为具有正确月份的行

这是我使用示例数据集所取得的成功

record_arr = example_df.to_records()

new_df = pd.DataFrame({'station': np.nan,
                  'year': np.nan,
                  'month':np.nan, 
                  'day': np.nan,
                  'tmax':np.nan,
                  'tmin': np.nan,
                  'prcp':np.nan},
                   index = [1]
                 )
new_df.append ({'station': record_arr[0][1], 'year': record_arr[0][2], 'month':record_arr[0][3], 'tmax':record_arr[0][5], 'tmin':record_arr[1][5] }, ignore_index = True)

这需要枢轴和熔化(或取消堆叠和堆叠)。我就是这样分两步得到它的

df1 = example_df.set_index(['station', 'year', 'month', 'attribute']).stack().reset_index()
df1.set_index(['station', 'year', 'month', 'level_4','attribute'])[0].unstack().reset_index()


attribute   station year    month   level_4 prcp    tmax    tmin
0           USC1    1993    1       day1    NaN     1.0     2.0
1           USC1    1993    1       day2    NaN     1.0     2.0
2           USC1    1993    1       day3    NaN     1.0     2.0
3           USC1    1993    1       day4    NaN     1.0     2.0
4           USC1    1993    2       day1    5.0     3.0     4.0
5           USC1    1993    2       day2    5.0     3.0     4.0
6           USC1    1993    2       day3    5.0     3.0     4.0
7           USC1    1993    2       day4    5.0     3.0     4.0
8           USC1    1993    3       day1    NaN     6.0     NaN
9           USC1    1993    3       day2    NaN     6.0     NaN
10          USC1    1993    3       day3    NaN     6.0     NaN
11          USC1    1993    3       day4    NaN     6.0     NaN

谢谢,您的解决方案与实际数据(NOAA提供的GHCN-DAILY station数据)完美配合。