Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I filter out subsets of a dataframe in python

this is my dataframe

import pandas as pd
df = pd.DataFrame({'Date' : ['2014-03-27', '2014-03-28', '2014-03-31', '2014-04-01', '2014-04-02', '2014-04-03', '2014-04-04', '2014-04-07', '2014-04-07', '2014-04-07'],
                   'income': [1849.04, 1857.62, 1872.34, 1885.52, 1890.9, 1888.77, 1865.09, 1845.04, 1235.04, 2323]

})
df.set_index('Date',inplace=True)
df = df.iloc[::-1]

enter image description here

How do I get the entire dataframe below a specific date.
I.e. if I specify "2014-04-07" then I want row containing "2014-04-07" and below.
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Likewise if I specify "2014-04-03", I want "2014-04-02" and below.

enter image description here

>Solution :

# boolean filter on index, and then loc to filter the rows
df.loc[df.index < '2014-04-07']
    income
Date    
2014-04-04  1865.09
2014-04-03  1888.77
2014-04-02  1890.90
2014-04-01  1885.52
2014-03-31  1872.34
2014-03-28  1857.62
2014-03-27  1849.04
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading