简体   繁体   中英

group by time by weekday and id column in python

I would like to group by 'time' column by weekday along with 'id' column. And here's how I tried and it throws an error:

AttributeError: 'Int64Index' object has no attribute 'weekday'.

Here's the code:

grouped_weekday_id = df_sample.set_index('time', drop=False, inplace=False)
grouped_weekday_id = grouped_weekday_id.groupby([df_sample.index.weekday, 'id']).mean().reset_index()

And sample data is as follows:

    time            id    64-digits     watts   temp
0   6/26/2017 0:25  1345  9.0072E+15    3.12    71.52
1   6/26/2017 0:25  144   0             1.2     72.96
...
2   6/26/2017 0:25  1152    
3   6/26/2017 0:25  1157    
4   6/26/2017 0:25  617     
5   6/26/2017 0:25  1328    
6   6/26/2017 0:25  913 
7   6/26/2017 0:25  1717

Here are the data types for df_sample dataframe.

time                datetime64[ns]
id                           int64
64-digits                    int64
watts                      float64
temp                       float64

As I understand it, you are assigning grouped_weekday_id to the df_sample dataframe with the time as the index.

Try this:

grouped_weekday_id = df_sample.set_index('time', drop=False, inplace=False)
grouped_weekday_id = grouped_weekday_id.groupby(by=grouped_weekday_id.index.weekday).mean().reset_index()

I changed the second line of code

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.

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