gpt4 book ai didi

python - 如何按月屏蔽数据框?

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:59 25 4
gpt4 key购买 nike

我有一个数据框df1,其中包含日期dates列。我想绘制某个月份的数据框。 日期 列如下所示:

   Unnamed: 0  Unnamed: 0.1      dates      DPD  weekday
0 0 1612 2007-06-01 23575.0 4
1 3 1615 2007-06-04 28484.0 0
2 4 1616 2007-06-05 29544.0 1
3 5 1617 2007-06-06 29129.0 2
4 6 1618 2007-06-07 27836.0 3
5 7 1619 2007-06-08 23434.0 4
6 10 1622 2007-06-11 28893.0 0
7 11 1623 2007-06-12 28698.0 1
8 12 1624 2007-06-13 27959.0 2
9 13 1625 2007-06-14 28534.0 3
10 14 1626 2007-06-15 23974.0 4

.. ... ... ... ... ...

513 721 2351 2009-06-09 54658.0 1
514 722 2352 2009-06-10 51406.0 2
515 723 2353 2009-06-11 48255.0 3
516 724 2354 2009-06-12 40874.0 4
517 727 2357 2009-06-15 77085.0 0
518 728 2358 2009-06-16 77989.0 1
519 729 2359 2009-06-17 75209.0 2
520 730 2360 2009-06-18 72298.0 3
521 731 2361 2009-06-19 60037.0 4
522 734 2364 2009-06-22 69348.0 0
523 735 2365 2009-06-23 74086.0 1
524 736 2366 2009-06-24 69187.0 2
525 737 2367 2009-06-25 68912.0 3
526 738 2368 2009-06-26 57848.0 4
527 741 2371 2009-06-29 72718.0 0
528 742 2372 2009-06-30 72306.0 1

我只想以 2007 年 6 月为例。

df1 = pd.read_csv('DPD.csv')
df1['dates'] = pd.to_datetime(df1['dates'])
df1['month'] = pd.PeriodIndex(df1.dates, freq='M')
nov_mask=df1['month'] == 2007-06

plot_data= df1[nov_mask].pivot(index='dates', values='DPD')
plot_data.plot()
plt.show()

我不知道我的代码出了什么问题。该错误表明,当我定义 nov_mask 时,2007-06 有问题,我认为数据类型是错误的,但我尝试了很多,但没有任何效果..

最佳答案

如果您只想获取 2007 年 6 月的数据,则不需要 PeriodIndex。我现在无法访问 IPython,但这应该会为您指明正确的方向。

df1 = pd.read_csv('DPD.csv')
df1['dates'] = pd.to_datetime(df1['dates'])
df1['year'] = df1['dates'].dt.year
df1['month'] = df1['dates'].dt.month

july_mask = ((df1['year'] == 2007) & (df1['month'] == 7))
filtered = df1[july_mask ]
# ... Do something with filtered.

关于python - 如何按月屏蔽数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57441565/

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