如何对年份和日期进行分组,以及汇总熊猫的总和?

时间:2020-09-19 16:47:31

标签: python pandas dataframe

Given Data

有人可以告诉我如何找到月收入,对其进行分类和可视化。

  Month&Year  |   Monthly Revenue
0   2016-11   |   261.9600
1   2016-11   |   731.9400
2   2016-06   |   14.6200
3   2015-10   |   957.5775
4   2015-10   |  22.3680
9989    2014-01  |  25.2480
9990    2017-02  |   91.9600
9991    2017-02  |  258.5760
9992    2017-02  |   29.6000
9993    2017-05  |   243.1600

如何显示不同年份的各个月的收入总额

1 个答案:

答案 0 :(得分:1)

import pandas as pd
import matplotlib.pyplot as plt

# setup dataframe
data = {'Month&Year': ['2016-11', '2016-11', '2016-06', '2015-10', '2015-10', '2014-01', '2017-02', '2017-02', '2017-02', '2017-05'],
        'Monthly Revenue': [261.96, 731.94, 14.62, 957.5775, 22.368, 25.248, 91.96, 258.576, 29.6, 243.16]}

df = pd.DataFrame(data)

# convert the Month&Year column to a datetime column
df['Month&Year'] = pd.to_datetime(df['Month&Year'], format='%Y-%m')

# use the .dt accessor to groupby year and month and sum Monthly Revenue
dfg = df.groupby([df['Month&Year'].dt.year, df['Month&Year'].dt.month]).agg({'Monthly Revenue': sum})

# rename the index columns
dfg.index = dfg.index.set_names(['year', 'month'])

# display(dfg)
            Monthly Revenue
year month                 
2014 1              25.2480
2015 10            979.9455
2016 6              14.6200
     11            993.9000
2017 2             380.1360
     5             243.1600

# plot
dfg.plot.barh(figsize=(8, 5), legend=False)
plt.xlabel('Revenue')
plt.xscale('log')
plt.show()

enter image description here

或者

  • 不是按yearmonth分组,而是按date分组。
# groupby 
dfg = df.groupby(df['Month&Year'].dt.date).agg({'Monthly Revenue': sum})

# plot
dfg.plot.barh(figsize=(8, 5), legend=False)
plt.xlabel('Revenue')
plt.ylabel('Date')
plt.xscale('log')
plt.show()

enter image description here