UserWarning: Pandas doesn't allow columns to be created via a new attribute name -

Viewed 7468

I am trying to rename columns after I read in a csv file where I want to replace a portion of the column name.
Here is my code where I want to remove :

'yor_yogurt_march_poggen_output_with_shrink.'

I get a user warning. I am using the pandas library if that matters.

df.dolumns =  df.columns.str.replace('yor_yogurt_march_poggen_output_with_shrink.', '')
2 Answers

The answer is in the title of your question: Pandas doesn't allow columns to be created via a new attribute name.

If your df does not have a column with the given name, you can not refer to this name using attribute notation (in this case df.dolumns).

You have to specify the new column name as an index, i.e.:

df['dolumns'] = ...

Another detail: After = you have df.columns which is a list of column names existing so far. As I understood, you want to perform the same replace in each column name (deleting the mentioned fragment). So maybe the resulting list of column names should be substituted under df.columns (statring with c not d)?

Double check df.dolumns is a typo in your post, but hopefully not in your code? Because it thinks you're trying to set a new attribute .dolumns for your df

Related