使用
query
用于筛选具有
1
以及用于总用途
GroupBy.sum
:
注意事项:
dropna
这里不能使用,因为
2
天数据列中的值。
s = (coupon_list.melt("DISCOUNT_PRICE")
.query('value == 1')
.groupby("variable")['DISCOUNT_PRICE']
.sum())
s.plot.bar()
您还可以更改日期顺序
reindex
或
ordered Categorical
S:
days = ['USABLE_DATE_MON', 'USABLE_DATE_TUE', 'USABLE_DATE_WED',
'USABLE_DATE_THU', 'USABLE_DATE_FRI', 'USABLE_DATE_SAT',
'USABLE_DATE_SUN']
s = (coupon_list.melt("DISCOUNT_PRICE")
.query('value == 1')
.groupby("variable")['DISCOUNT_PRICE']
.sum()
.reindex(days))
days = ['USABLE_DATE_MON', 'USABLE_DATE_TUE', 'USABLE_DATE_WED',
'USABLE_DATE_THU', 'USABLE_DATE_FRI', 'USABLE_DATE_SAT',
'USABLE_DATE_SUN']
s = (coupon_list.melt("DISCOUNT_PRICE", var_name='days', value_name='data')
.assign(days = lambda x: pd.Categorical(x['days'],
ordered=True,
categories=days))
.query('value == 1')
.groupby("days")['DISCOUNT_PRICE']
.sum())
样品
:
coupon_list = pd.DataFrame({
'USABLE_DATE_MON':[np.nan,np.nan,np.nan,1,1,np.nan],
'USABLE_DATE_TUE':[1,np.nan,1,np.nan,1,np.nan],
'USABLE_DATE_WED':[1,np.nan,np.nan,np.nan,1,1],
'USABLE_DATE_THU':[1,1,np.nan,1,1,np.nan],
'USABLE_DATE_FRI':[np.nan,1,2,np.nan,1,np.nan],
'USABLE_DATE_SAT':[1,1,np.nan,1,1,2],
'USABLE_DATE_SUN':[1,np.nan,1,1,1,1],
'DISCOUNT_PRICE':[2,3,6,2,2,4],
})
print (coupon_list)
USABLE_DATE_MON USABLE_DATE_TUE USABLE_DATE_WED USABLE_DATE_THU \
0 NaN 1.0 1.0 1.0
1 NaN NaN NaN 1.0
2 NaN 1.0 NaN NaN
3 1.0 NaN NaN 1.0
4 1.0 1.0 1.0 1.0
5 NaN NaN 1.0 NaN
USABLE_DATE_FRI USABLE_DATE_SAT USABLE_DATE_SUN DISCOUNT_PRICE
0 NaN 1.0 1.0 2
1 1.0 1.0 NaN 3
2 2.0 NaN 1.0 6
3 NaN 1.0 1.0 2
4 1.0 1.0 1.0 2
5 NaN 2.0 1.0 4
days = ['USABLE_DATE_MON', 'USABLE_DATE_TUE', 'USABLE_DATE_WED',
'USABLE_DATE_THU', 'USABLE_DATE_FRI', 'USABLE_DATE_SAT',
'USABLE_DATE_SUN']
s = (coupon_list.melt("DISCOUNT_PRICE", var_name='days', value_name='data')
.assign(days = lambda x: pd.Categorical(x['days'],
ordered=True,
categories=days))
.query('data == 1')
.groupby("days")['DISCOUNT_PRICE']
.sum())
print (s)
days
USABLE_DATE_MON 4
USABLE_DATE_TUE 10
USABLE_DATE_WED 8
USABLE_DATE_THU 9
USABLE_DATE_FRI 5
USABLE_DATE_SAT 9
USABLE_DATE_SUN 16
Name: DISCOUNT_PRICE, dtype: int64
s.plot.bar()