简体   繁体   English

如何在Python中按月逐月绘制日期值的直方图?

[英]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 docstringby参数为:

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.

相关问题 如何每年制作带有标签的年份值直方图? - How can I make a histogram of year values with labels every year? 如何使用Python / Pandas从“日期”字段按月分组 - How can I Group By Month from a Date field with Python/Pandas 如何使用 python 从 sql 查询日期值,其中我只有年和月 - How to query date values from sql using python where I only have the year and month 如何按(月+年)转换为 Pandas dataframe 进行分组? - How can I make a group by (month + year) converting into a Pandas dataframe? 如何将日期从日-月-年重新格式化为年-月-日? - How can I reformat date from day-month-year to year-month-day? 如何将此日期格式化为日 - 月 - 年 - How can I format this date into Day - Month - Year 说我的日期格式不匹配。 另外,如何将这些日期格式化为年/月/日(python) - Says that I date formatting does not match. Also, how can I format these dates into year/month/date (python) 对于每个组,如果最近一年具有特定值,我如何替换前几年的值? - For each group, how can I replace values for previous years if a recent year has a certain value? 如何将星期几、月份、日期转换为年 - 月 - 日期 - How can I convert day of week, Month, Date to Year - Month - Date 如何从 python 中的日期中提取年份和月份 - How to extract year and month from date in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM