Data contains
timeslot Weather Location Slot
2014-10-26 00:00 35 1 1
2014-10-26 06:00 36 1 2
2014-10-26 12:00 34 1 3
2014-10-26 18:00 34 1 4
2014-10-27 00:00 35 1 1
2014-10-27 06:00 36 1 2
2014-10-27 12:00 36 1 3
2014-10-27 18:00 32 1 4
2014-10-28 00:00 35 1 1
2014-10-28 06:00 33 1 2
2014-10-28 12:00 35 1 3
2014-10-28 18:00 33 1 4
2014-10-26 00:00 45 2 1
2014-10-26 06:00 46 2 2
2014-10-26 12:00 41 2 3
2014-10-26 18:00 39 2 4
2014-10-27 00:00 46 2 1
2014-10-27 06:00 44 2 2
2014-10-27 12:00 45 2 3
2014-10-27 18:00 42 2 4
2014-10-28 00:00 41 2 1
2014-10-28 06:00 40 2 2
2014-10-28 12:00 42 2 3
2014-10-28 18:00 41 2 4
Data contains weather of two location point. An deach day is converted to 6 hr time slots. I want to convert the data to pivot table.
The code i tried is
df.pivot(index='Location', columns='Timeslot', values='weather')
Output should be :
Timeslot 2014-10-26 || 2014-10-27 || 2014-10-28
---------------------------------------------------------------------------
slot 1 2 3 4 || 1 2 3 4 || 1 2 3 4
---------------------------------------------------------------------------
Location
1 35 36 34 34 35 36 32 32 35 33 35 33
2 45 46 41 39 46 44 45 42 41 40 42 41
Use DataFrame.set_index
with DataFrame.unstack
and for dates use Series.dt.date
:
df['timeslot'] = pd.to_datetime(df['timeslot'])
df = df.set_index(['Location', df['timeslot'].dt.date, 'Slot'])['Weather'].unstack([1,2])
print (df)
timeslot 2014-10-26 2014-10-27 2014-10-28
Slot 1 2 3 4 1 2 3 4 1 2 3 4
Location
1 35 36 34 34 35 36 36 32 35 33 35 33
2 45 46 41 39 46 44 45 42 41 40 42 41
If possible duplicates in combinations (triples Location
, dates of timeslot
and Slot
) is necessary aggregation by DataFrame.pivot_table
:
df = df.pivot_table(index='Location',
columns=[df['timeslot'].dt.date, 'Slot'],
values='Weather',
aggfunc='mean')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.