I have a question, I am trying to get more familiar with python and pandas and I was wondering why this does not work:
list_features_countries = [features_AU, features_CA, features_UK, features_US, features_JP, features_DE, features_SW]
for x in list_features_countries:
x = x.drop(columns=x.columns[0], axis=1, inplace=True)
x = x.insert(0,'time_period', Eco_AU['time_period'])
(It gave the error: AttributeError: 'NoneType' object has no attribute 'insert')
But, this does work:
list_features_countries = [features_AU, features_CA, features_UK, features_US, features_JP, features_DE, features_SW]
for x in list_features_countries
x = x.drop(columns=x.columns[0], axis=1, inplace=True)
for x in list_features_countries:
x = x.insert(0,'time_period', Eco_AU['time_period'])
So basically I tried to remove the first column of every dataframe in the list and then add a column of another dataframe.
CodePudding user response:
The problem with the first variant is the parameter inplace=True
.
This means that the existing dataframe (x) is changed in place and the call x.drop(... inplace=True)
returns None.
So x is assigned None at this point, which leads to the error message in the next step.
The solution would be either to remove the inplace=True
parameter so that the x.drop(...)
function returns the edited dataframe:
x = x.drop(columns=x.columns[0], axis=1)
x = x.insert(0,'time_period', Eco_AU['time_period'])
or to remove the assignment:
x.drop(columns=x.columns[0], axis=1, inplace=True)
x = x.insert(0,'time_period', Eco_AU['time_period'])