简体   繁体   English

我怎样才能在 dataframe 中为 groupby 做 Python

[英]How can I do Python for groupby in dataframe

I have one dataframe that includes transactions.我有一个 dataframe,其中包含交易。 I want to group data.我想对数据进行分组。

buy_date            category    subcategory product         actual_price    sell_price
1/1/2021            Cloth       women       shirt style A   5               4
1/1/2021            Cloth       men         skirt style A   7               6.5
1/1/2021            Accessories ear         sky wing        2               1
2/1/2021            Automotive  wheel       small           21              18
2/1/2021            Automotive  wheel       big             34              30
1/14/2021           Accessories ring        queen couple    3               3
1/17/2021           Cloth       women       shirt style B   7               7
1/17/2021           Cloth       men         skirt style A   7               6.5
4/2/2021            Cloth       men         skirt style A   10              9
5/2/2021            Accessories ring        queen couple    3               2.5
7/2/2021            Cloth       women       shirt style B   16              12
7/2/2021            Automotive  wheel       big             40              35
2/26/2021           Accessories ring        queen couple    4               4
2/26/2021           Cloth       women       shirt style B   9               5
2/26/2021           Cloth       men         skirt style A   7               9
2/28/2021           Accessories ear         sky wing        2               1
1/3/2021            Automotive  wheel       big             38              35
1/3/2021            Accessories ring        queen couple    4               4
7/3/2021            Automotive  wheel       big             39              37
3/31/2021           Accessories ring        queen couple    4               4

I want to get the average monthly sell and the actual price per category and subcategory.我想获得每个类别和子类别的平均每月销售量和实际价格。 I've tried many approaches, but they were not working properly.我尝试了很多方法,但它们都无法正常工作。 Thanks谢谢

Just do:做就是了:

>>> df.groupby(['category', 'subcategory']).mean()
                         actual_price  sell_price
category    subcategory                          
Accessories ear                  2.00        1.00
            ring                 3.60        3.50
Automotive  wheel               34.40       31.00
Cloth       men                  7.75        7.75
            women                9.25        7.00
>>> 

You can find month then groupby with three desired columns.您可以找到month ,然后使用三个所需的列进行groupby

Try this:尝试这个:

df['month'] = pd.to_datetime(df["buyDate"]).dt.month

df.groupby(['month','category', 'subcategory']).mean()

Use Grouper with aggregate mean :Grouper与聚合mean一起使用:

df['buyDate'] = pd.to_datetime(df["buyDate"])
df.groupby([pd.Grouper(freq='M', key='buyDate'),'category', 'subcategory']).mean()

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

相关问题 我如何在 Python 中的数据帧上使用 groupby 函数 - How do i use the groupby function on dataframe in Python 如何在 Python 中对这个 DataFrame 进行操作自定义 this.groupby 的 output? - How can I customize the output of this .groupby operation done on this DataFrame in Python? 如何在熊猫python中分组,但保留数据框的顺序? - How can I groupby in pandas python but conserving the order of the dataframe? 如何将Pandas DataFrame groupby结果转换为DataFrame? - How do I turn pandas DataFrame groupby results into a DataFrame? 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python? 如何在python中的数据框列上使用正则表达式进行分组? - How to do a groupby with a regex over a dataframe column in python? 如何使用groupby执行此操作? - How can I do this by using groupby? Python如何将分组分组为合并的数据帧? - Python how to groupby into colsolidated dataframe? 应用size()之后如何在DataFrame Groupby上进行迭代? - How do I iterate over DataFrame Groupby after applying size()? 如何通过 groupby 计算 pandas dataframe 中的更改数量? - How do I count # of changes in pandas dataframe by groupby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM