简体   繁体   English

查找一个月的每周和周末平均销售额

[英]Find weekly and weekend average sales of a month

I'm trying to compare average sales of a weekend and weekday in python.我试图在 python 中比较周末和工作日的平均销售额。

Suppose I have a dataset假设我有一个数据集

 Order Date  Units Sold day_week
2017-07-01  100 Sat
2017-07-02  100 Sun
2017-07-03  90  Mon
2017-07-04  90  Tue
2017-07-05  90  Wed
2017-07-06  90  Thu
2017-07-07  90  Fri
2017-07-08  80  Sat
2017-07-09  80  Sun
2017-07-10  100 Mon
2017-07-11  100 Tue
2017-07-12  100 Wed
2017-07-13  100 Thu
2017-07-14  100 Fri

I want to compare (average sales of weekend that is sat and sun) with (average sales of weekdays), but individually like (1st and 2nd with 3,4,5,6,7 sales) and (8,9 with 10,11,12,13,14)我想比较(周六和周日的周末平均销售额)与(工作日的平均销售额),但分别喜欢(第一和第二,3,4,5,6,7 销售额)和(8,9 和 10, 11,12,13,14)

So in 1st week, weekend average sales (100) will be more than weekday average sales(90) and in 2nd week , weekend average sales (80) will be less than weekday average sales (100)因此,在第一周,周末平均销售额 (100) 将高于工作日平均销售额 (90),而在第二周,周末平均销售额 (80) 将低于工作日平均销售额 (100)

Ok, here's assuming your data is in a DataFrame format, but the date/time is simple str (ie not datetime ):好的,这里假设您的数据采用DataFrame格式,但日期/时间很简单str (即不是datetime ):

import pandas as pd

# setting up part of your dataset

df = pd.DataFrame.from_dict({
    'date':['2017-07-01','2017-07-02','2017-07-03','2017-07-04'],
    'units_sold': [100,100,90,90],
    'day_week': ['Sat','Sun','Mon','Tue']}
)

# defining a new column to help us, grouping by it and then summing:

df['is_weekend']=df['day_week'].apply(lambda x: x in {'Sat','Sun'})
df.groupby('is_weekend').mean()

Also, in the future, it's good conduct to write the code that generates your dataset (or a small part of it), otherwise the reader has to do it itself.此外,将来最好编写生成数据集(或其中一小部分)的代码,否则读者必须自己编写。

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

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