In a dataset containing temperatures of different years and I want to extract data of particular month from all different years in single liner code what is the syntax? To extract all the data from a single month the syntax is :df['Temperature'].loc['2010-08']
It is always year followed by month.What if I want to extract 2010 & 2011 years temperatures in August in single line.
$\begingroup$
$\endgroup$
1
-
1$\begingroup$ Kindly reframe your question a little bit more.. thanks.., Simply add a new column of month,year etc.. and then the problem becomes trivial, isn't.... Spoiler use group to get the statistics air do what ever you want.. shanelynn.ie/… $\endgroup$– AdityaCommented May 25, 2018 at 8:15
Add a comment
|
2 Answers
$\begingroup$
$\endgroup$
2
df.loc[df['Temperature'].isin(['2010-08', '2011-08'])]
isin() will help in setting filter for multiple values.
The other way is to use regex to extract data of particular month from different years:
import re
df['Temperature'].filter(lambda x: re.match('-08$', x))
It will check if the value ends with -08 (in this case, if the month is August) and select only such records.
Hope it helps!
-
$\begingroup$ I don't want to explicitly mention the year $\endgroup$ Commented May 25, 2018 at 7:50
-
$\begingroup$ Okay. You need to use regex in that case. I'll update in the answer. $\endgroup$ Commented May 25, 2018 at 8:26
$\begingroup$
$\endgroup$
You can use between
over the required months for filtering, based on you datetime
column:
Aug= df['datetime'].dt.month.between(8,8)
df_Aug = df.loc[Aug]