How do you drop multiple columns by a conditional index value? For example, I'd like to drop all columns that are greater than index number 40.
1 Answer
Assuming that you have a DataFrame like this:
df = pd.DataFrame(np.arange(120).reshape(60, 2),
columns=['A', 'B'])
You can drop the columns when the index is greater than a specified value (for instance 40):
value=40
rows_to_drop=df.index[np.where(df.index>value)[0]]
df=df.drop(rows_to_drop,axis=0)
df now has the columns that have an index less than 40.
I hope I have understood your question.