简体   繁体   English

数据到数据透视表

[英]Data to pivot table

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. 每天的停留时间转换为6小时的时间段。 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 : DataFrame.set_indexDataFrame.unstack DataFrame.set_index使用,对于日期,请使用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 : 如果可能的话,请使用DataFrame.pivot_table组合中的重复项(三重Locationtimeslot日期和Slot )进行汇总:

df = df.pivot_table(index='Location', 
                    columns=[df['timeslot'].dt.date, 'Slot'],
                    values='Weather', 
                    aggfunc='mean')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM