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.
>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