使用数据透视表重新采样和重新分组

时间:2020-04-25 11:03:46

标签: python pandas pivot-table resampling

已编辑---已添加代码

我正在尝试按天(和年份)对数据框essaie ['night_cons']的所有值进行分组,但结果只是给出了NAN。


colss = {'Date_Time': ['2017-11-10','2017-11-11','2017-11-12','2017-11-13', '2017-11-14', '2017-11-15', '2017-11-16', '2017-11-17', '2017-11-18', '2017-11-19'],
        'Night_Cons(+)': [4470.76,25465.72,25465.72,25465.72, 21480.59, 20024.53, 19613.29, 28015.18, 28394.20, 29615.69]
        }

dataframe = pd.DataFrame(colss, columns = ['Date_Time', 'Night_Cons(+)'])

#print (dataframe)

dataframe['Date_Time'] = pd.to_datetime(dataframe['Date_Time'], errors = 'coerce')

# Create new columns
dataframe['Day'] = dataframe['Date_Time'].dt.day
dataframe['Month'] = dataframe['Date_Time'].dt.month
dataframe['Year'] = dataframe['Date_Time'].dt.year

# Set index
#essaie = essaie.set_index('Date_Time') 

dataframe = dataframe[['Night_Cons(+)', 'Day', 'Month', 'Year']]

#dataframe

#daily_data = pd.pivot_table(essaie, values = "Night_Cons(+)", columns = ["Month"], index = "Day")
daily_data = pd.pivot_table(dataframe, values = "Night_Cons(+)", columns = ["Year"], index = "Day")
daily_data = daily_data.reindex(index = ['Montag','Dienstag','Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'])

daily_data

DataFrame and Results

请参见下图。

1 个答案:

答案 0 :(得分:1)

示例

colss = {'Date_Time': ['2017-11-10','2017-11-11','2017-11-12','2017-11-13', '2017-11-14', '2017-11-15', '2017-11-16', '2017-11-17', '2017-11-18', '2017-11-19'],
        'Night_Cons(+)': [4470.76,25465.72,25465.72,25465.72, 21480.59, 20024.53, 19613.29, 28015.18, 28394.20, 29615.69]
        }

dataframe = pd.DataFrame(colss, columns = ['Date_Time', 'Night_Cons(+)'])

首先将Date列转换为Series.dt.dayofweek,然后旋转并最后重命名索引值:

dataframe['Date_Time'] = pd.to_datetime(dataframe['Date_Time'], errors = 'coerce')
dataframe['Year'] = dataframe['Date_Time'].dt.year
dataframe['Date'] = dataframe['Date_Time'].dt.dayofweek
daily_data = dataframe.pivot_table(values = "Night_Cons(+)", 
                                   columns = "Year",
                                   index = "Date")

days = ['Montag','Dienstag','Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']

daily_data = daily_data.rename(dict(enumerate(days)))
print (daily_data)
Year             2017
Date                 
Montag      25465.720
Dienstag    21480.590
Mittwoch    20024.530
Donnerstag  19613.290
Freitag     16242.970
Samstag     26929.960
Sonntag     27540.705