如何在多个条件下对Pandas数据进行分组?

时间:2018-01-25 12:10:31

标签: python pandas

这是我的表

timestamp        date month  day   hour   price
0  2017-01-01 00:00  01/01/2017   Jan  Sun  00:00  60.23
1  2017-01-01 01:00  01/01/2017   Jan  Sun  01:00  60.73
2  2017-01-01 02:00  01/01/2017   Jan  Sun  02:00  75.99
3  2017-01-01 03:00  01/01/2017   Jan  Sun  03:00  60.76
4  2017-01-01 04:00  01/01/2017   Jan  Sun  04:00  49.01

我每天24小时都有数据,每天都有一整年的数据。

例如,我希望将每个季节的数据分组为工作日和周末 Weekend_Winter =所有星期六和星期日的数据,分别为11月,12月,1月,2月

相当新手,所以任何帮助都会有用

2 个答案:

答案 0 :(得分:1)

如果希望按条件筛选数据,请使用boolean indexing创建的布尔掩码,将dayofweekisin进行比较,以获得列表L中的检查成员资格:

#changed timestamp values only for better sample
print (df)
            timestamp        date month  day   hour  price
0 2017-01-01 00:00:00  01/01/2017   Jan  Sun  00:00  60.23
1 2017-01-03 00:00:00  01/01/2017   Jan  Sun  00:00  60.23
2 2017-02-01 01:00:00  01/01/2017   Jan  Sun  01:00  60.73
3 2017-02-05 01:00:00  01/01/2017   Jan  Sun  01:00  60.73
4 2017-03-01 02:00:00  01/01/2017   Jan  Sun  02:00  75.99
5 2017-04-01 03:00:00  01/01/2017   Jan  Sun  03:00  60.76
6 2017-11-01 04:00:00  01/01/2017   Jan  Sun  04:00  49.01

L = ['Nov','Dec','Jan','Feb']
mask = (df['timestamp'].dt.dayofweek > 4) & (df['month'].isin(L))
df1 = df[mask]
print (df1)
            timestamp        date month  day   hour  price
0 2017-01-01 00:00:00  01/01/2017   Jan  Sun  00:00  60.23
3 2017-02-05 01:00:00  01/01/2017   Jan  Sun  01:00  60.73
5 2017-04-01 03:00:00  01/01/2017   Jan  Sun  03:00  60.76

如果需要season的新列和日期类型:

df['season'] = (df['timestamp'].dt.month%12 + 3) // 3
df['state'] = np.where(df['timestamp'].dt.dayofweek > 4, 'weekend','weekdays')
print (df)
            timestamp        date month  day   hour  price  season     state
0 2017-01-01 00:00:00  01/01/2017   Jan  Sun  00:00  60.23       1   weekend
1 2017-01-03 00:00:00  01/01/2017   Jan  Sun  00:00  60.23       1  weekdays
2 2017-02-01 01:00:00  01/01/2017   Jan  Sun  01:00  60.73       1  weekdays
3 2017-02-05 01:00:00  01/01/2017   Jan  Sun  01:00  60.73       1   weekend
4 2017-03-01 02:00:00  01/01/2017   Jan  Sun  02:00  75.99       2  weekdays
5 2017-04-01 03:00:00  01/01/2017   Jan  Sun  03:00  60.76       2   weekend
6 2017-11-01 04:00:00  01/01/2017   Jan  Sun  04:00  49.01       4  weekdays

可以将groupby用于聚合,例如sum

df2 = df.groupby(['season','state'], as_index=False)['price'].sum()
print (df2)
   season     state   price
0       1  weekdays  120.96
1       1   weekend  120.96
2       2  weekdays   75.99
3       2   weekend   60.76
4       4  weekdays   49.01

答案 1 :(得分:1)

下面的解决方案与@jezrael在那个季节略有不同,工作日明确定义。

import pandas as pd

df = pd.DataFrame([['2017-01-01 00:00', '01/01/2017', 'Jan', 'Mon', '00:00', 60.23],
                   ['2017-01-01 01:00', '01/01/2017', 'Jan', 'Sat', '01:00', 60.73],
                   ['2017-01-01 02:00', '01/01/2017', 'May', 'Tue', '02:00', 75.99],
                   ['2017-01-01 03:00', '01/01/2017', 'Jan', 'Sun', '03:00', 60.76],
                   ['2017-01-01 04:00', '01/01/2017', 'Sep', 'Sat', '04:00', 49.01]],
                   columns=['timestamp', 'date', 'month', 'day', 'hour', 'price'])

def InvertKeyListDictionary(input_dict):
    return {w: k for k, v in input_dict.items() for w in v}

season_map = {'Spring': ['Mar', 'Apr', 'May'],
              'Summer': ['Jun', 'Jul', 'Aug'],
              'Autumn': ['Sep', 'Oct', 'Nov'],
              'Winter': ['Dec', 'Jan', 'Feb']}

weekend_map = {'Weekday': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
               'Weekend': ['Sat', 'Sun']}

month_map = InvertKeyListDictionary(season_map)
day_map = InvertKeyListDictionary(weekend_map)

df['season'] = df['month'].map(month_map)
df['daytype'] = df['day'].map(day_map)

df_groups = df.groupby(['season', 'daytype'])

df_groups.get_group(('Winter', 'Weekend'))

# output
# timestamp date month day hour price season daytype
# 2017-01-01 01:00 01/01/2017 Jan Sat 01:00 60.73 Winter Weekend 
# 2017-01-01 03:00 01/01/2017 Jan Sun 03:00 60.76 Winter Weekend