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 to drop rows based on value of index

I want to drop rows based on index values, but I don’t mean listing them. I want to see if there is a way where I could not list down the years manually.

I want to drop the rows with indices below 2000, is there anyway to do this with a formula, im thinking something like drop(label=[df.index<2000].

obviously the code is incorrect but i hope it gives an Idea of what I want to happen.

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

Copy of table

>Solution :

To select all indexes with an value greater than 2000, you can use df.index>2000. To filter for greater or equal use df.index>=2000. This will reduce the original DataFrame and drop all values with a smaller index. To see the difference, you can create a copy and compare with the original data.

import pandas as pd
df = pd.DataFrame({'a':[0,1,2,3,4]}, index=[1998.0,1999,2000,2001,2002])
dropped_df = df[df.index>2000].copy()
>>> dropped_df
        a
2001.0  3
2002.0  4

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