Reputation: 696
I have a dataframe like below :
| Date | Value |
+----------+--------+
|2009-01-01| 34 |
|2009-01-03| 45 |
|2009-02-03| 56 |
|2009-02-18| 57 |
|2009-03-17| 59 |
|2009-03-26| 60 |
|2010-01-01| 23 |
|2010-01-17| 34 |
|2010-02-14| 23 |
|2010-02-18| 42 |
|2010-03-21| 12 |
|2010-03-29| 11 |
+----------+--------+
I want to filter the data frame to include values of only January month and not other months, which should like below.
| Date | Value |
+----------+--------+
|2009-01-01| 34 |
|2009-01-03| 45 |
|2010-01-01| 23 |
|2010-01-17| 34 |
+----------+--------+
So, how to filter such dataframes using date column ? Thanks,
Upvotes: 1
Views: 42
Reputation: 18315
Iff the pattern is fixed, a way is to check 5th and 6th characters:
df[df.Date.str[5:7] == "01"]
to get
Date Value
1 2009-01-01 34.0
2 2009-01-03 45.0
7 2010-01-01 23.0
8 2010-01-17 34.0
Upvotes: 1
Reputation: 150815
You can convert the date into datetime type with
df['Date'] = pd.to_datetime(df['Date'])
then use dt.month
:
df[df['Date'].dt.month==1]
Upvotes: 1