Have data on temperature that spans multiple years in 2010-01-01 format. I want to isolate the temps from June and am unsure how to filter this. Typically the method I use would be df[df['date'] == 2016]
but this only parses out by year.
IIUC, you can use a datetime method ontop of your datetime to access the month
impot pandas as pd
rng = pd.date_range('2010-01-01','2011-01-01',freq='D')
df = pd.DataFrame({'dates':rng})
print(df.head(5))
dates
0 2010-01-01
1 2010-01-02
2 2010-01-03
3 2010-01-04
4 2010-01-05
.loc
accessor to filter the dataframe with a dt.month method:df.loc[df['dates'].dt.month == 2]
dates
31 2010-02-01
32 2010-02-02
33 2010-02-03
34 2010-02-04
35 2010-02-05
36 2010-02-06
Ensure your date is a proper datetime object by using pd.to_datetime
use print(df.dtypes)
to check the datatypes.
您可以使用方法dt.month_name()
df[df['date'].dt.month_name() == 'June']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.