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.