[英]How can I draw the histogram of date values group by month in each year in Python?
I wrote this code to draw the histogram of date values in each month. 我写了这段代码来绘制每个月中日期值的直方图。 It shows the number of dates for each month in the whole dataset.
它显示了整个数据集中每个月的日期数。 But I want the histogram to be for each month in each year.That is, for example, I should have January through December for year1, and then January through December for year2 and so on.
但是我希望直方图是每年的每个月,也就是说,例如,对于year1,我应该有1月到12月,然后对于year2应该是1月到12月,依此类推。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
pd.options.display.mpl_style = 'default'
sns.set_context("talk")
df = pd.read_csv("data.csv", names=['lender','loan','country','sector','amount','date'],header=None)
date=df['date']
df.date = date.astype("datetime64")
df.groupby(df.date.dt.month).count().plot(kind="bar")
According to the docstring the groupby
docstring , the by
parameter is: 根据文档字符串
groupby
docstring , by
参数为:
list of column names.
列名列表。 Called on each element of the object index to determine the groups.
调用对象索引的每个元素以确定组。 If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups
如果通过了dict或Series,则将使用Series或dict VALUES来确定组
So your code simply becomes: 因此,您的代码将变成:
df = pd.read_csv(...)
df['date'] = df['date'].astype("datetime64")
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df.groupby(by=['month', 'year']).count().plot(kind="bar")
But I would write this as: 但我会这样写:
ax = (
pandas.read_csv(...)
.assign(date=lambda df: df['date'].astype("datetime64"))
.assign(year=lambda df: df['date'].dt.year)
.assign(month=lambda df: df['date'].dt.month)
.groupby(by=['year', 'month'])
.count()
.plot(kind="bar")
)
And now you have a matplotlib axes object that you can use to modify the tick labels (eg, matplotlib x-axis ticks dates formatting and locations ) 现在,您有了一个matplotlib axes对象,可用于修改刻度标签(例如, matplotlib x轴刻度日期格式和位置 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.