0

My Dataframe looks like this

 2013-12-25 |
 2013-12-25 |
 2013-12-25 |
 2013-12-25 |
 2013-12-25 |
 ....
 ....
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |
 2014-01-01 |

I have to select all the rows with year 2014 and month as 01.

How can I go ahead?

Cœur
  • 37,241
  • 25
  • 195
  • 267
itsaruns
  • 659
  • 2
  • 11
  • 16
  • Are they strings or actual dates? Are they the index, or the first column? Have you read [the documentation](http://pandas.pydata.org/pandas-docs/dev/indexing.html)? What code have you written so far? – jonrsharpe Jan 28 '14 at 08:16
  • They are Date values .and sadly i couldnot find any help with the documentation. – itsaruns Jan 28 '14 at 08:39
  • This may be useful: http://stackoverflow.com/questions/11991627/selecting-a-subset-of-a-pandas-dataframe-indexed-by-datetimeindex-with-a-list-of – jonrsharpe Jan 28 '14 at 08:51

1 Answers1

3

This probably isn't the most Pythonic way but you could just slice on dates.

df[(df['datecol'] >= pd.datetime(2014, 1, 1)) & (df['datecol'] <= pd.datetime(2014, 1, 31))]

Alternatively you could create a map for the criteria to do boolean selection on month and year.

crit1 = df['datecol'].map(lambda x : x.year == 2014)
crit2 = df['datecol'].map(lambda x : x.month == 1)

df[crit1 & crit2]
jmz
  • 4,138
  • 28
  • 27
  • 1
    Thanks, more compact solution: ```df[df['datecol'].map(lambda x : (x.year == 2014) & (x.month == 1))]``` – Marius Aug 05 '21 at 11:47