0
$\begingroup$

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.

$\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$
    – Aditya
    Commented May 25, 2018 at 8:15

2 Answers 2

1
$\begingroup$
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!

$\endgroup$
2
  • $\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$
    – aathiraks
    Commented May 25, 2018 at 8:26
0
$\begingroup$

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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.