Home > OS >  How do I filter out subsets of a dataframe in python
How do I filter out subsets of a dataframe in python

Time:11-11

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

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

enter image description here

CodePudding user response:

# 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
  • Related