gpt4 book ai didi

python - 如何从 Pandas 数据框值中计算特定日期间隔内的值数?

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:34 24 4
gpt4 key购买 nike

我想从这个数据框中计算并绘制 2018-04-01 到 2018-05-01 期间每天“支出”值的数量:

df['payout'].head(10)

0 2017-02-14 11:00:06
1 2015-03-14 11:00:06
2 2014-04-14 11:00:06
3 2017-11-14 11:00:06
4 2016-12-14 11:00:06
5 2018-04-10 11:00:06
6 2018-04-11 11:00:06
7 2018-04-12 11:00:06
8 2018-04-13 11:00:06
9 2018-04-14 11:00:06

我可以获得 2018 年的日间绘图:

(df.loc[df['payout'].dt.year.between(2018, 2019), 'payout']
.dt.to_period('D')
.value_counts()
.sort_index()
.plot(kind="bar")
)

enter image description here

如何将情节缩小到 2018 年 4 月?

谢谢

最佳答案

只需检查月份和年份并使用您当前的方法

df.loc[(df.date.dt.month == 4) & (df.date.dt.year == 2018), 'payout']

关于python - 如何从 Pandas 数据框值中计算特定日期间隔内的值数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50593341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com