user40
user40

Reputation: 1400

How to count number of values within specific date interval from pandas dataframe values?

I want to count and plot the number of 'payout' values by day for period 2018-04-01 to 2018-05-01 from this dataframe:

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

I could obtain day-to-plot for the year 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

How do I shrink the plot to April 2018 only?

Thanks

Upvotes: 1

Views: 823

Answers (4)

Joooeey
Joooeey

Reputation: 3866

df.set_index('payout').loc['2018-04-01':'2018-04-30']

for your first line should do it.

  • .set_index makes your payout column the index. This does not modify the original df. See the docs for details.
  • Now that you have a DatetimeIndex, you can just use .loc to index with date strings directly. Note that unlike normal indexing, this will include all 24 hours of April 30.

Upvotes: 1

Mohamed AL ANI
Mohamed AL ANI

Reputation: 2052

an other solution :

df[(df['date'] >= '2018-04-01') & (df['date'] < '2018-05-01')]['payout']

Upvotes: 1

sacuL
sacuL

Reputation: 51335

You could use the same logic you were using, but use the datestrings you are interested in:

(df.loc[df['payout'].between('2018-04-01', '2018-04-30'), 'payout']
         .dt.to_period('D')
         .value_counts()
         .sort_index()
         .plot(kind="bar")
)

Upvotes: 1

user3483203
user3483203

Reputation: 51165

Just check both month and year and use your current method

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

Upvotes: 3

Related Questions