1

I have multiple dataframes that should all have the same column headers and dimensions. I'm trying to drop the same columns from each one as an iterative process, rather than doing it one by one.

So I have a list of my data frames:

groupDF = [df1, df2, df3]

Then I've tried this to remove the columns, just starting by removing 1 column:

for i in groupDF:
    i.columns = ['firstname', 'lastname', 'age', 'sex']
    i.drop(i.columns[2])

The renaming of the columns part works fine on its own. However, I get this error when I try to drop a column:

 "labels ['age'] not contained in axis"

From what I can find, this error can happen if the columns aren't named correctly. However, if I run:

df1.columns

It shows the names that I would expect for each data frame. It also confuses me because I haven't stated the name of the column, only the position.

When I try to specify the name (by replacing 2 with ['age'] ) to drop instead, I get this:

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and
integer or boolean arrays are valid indices

Maybe I'm missing a much easier way of doing this. I appreciate any help someone might be able to give.

0

2 Answers 2

2

The default behavior of pandas.DataFrame.drop is that axis=0. This means that the method will try to drop labels in the 0th axis i.e. the DataFrame index. If you want to drop columns you need to specify that the axis to drop from is axis=1. In your example this would look as follows:

 i.drop(i.columns[2], axis=1, inplace=True)
Sign up to request clarification or add additional context in comments.

2 Comments

Well that was silly, thank you. Now I just need to get it to work on multiple columns. Using [2,3] brings a different error
why not try i.drop(i.columns[2:4], axis=1, inplace=True) for that?
1

Thank you Scott. For anyone stumbling across this, this was my final solution.

for i in groupDF:
    i.columns = ['firstname', 'lastname', 'age', 'sex']
    i.drop(i.columns[[2,3]], axis=1, inplace=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.