0

If a pandas dataframe has no spaces or special characters for column names the following command works great:

df.loc[df.Weight == "155", "Name"] = "John"

But what if the column name has special characters such as % and spaces are part of a column name. Replace Weight in the previous command with:

% Population

What would the command be?

If I use the escape character , I receive a line continuation error. If I place the name in quotes, I get invalid syntax. The only way that I have found to run this command is to rename the column to a name without special characters, run the command, then rename the column back to its original name.

Thanks.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45

1 Answers1

0

You can't use dot notation with all names. You should use brackets instead.`

df.loc[df['% Population'] == "155", "Name"] = "John"`
Matthew Borish
  • 3,016
  • 2
  • 13
  • 25