0

I have a dataset dfthat looks like this:

code
Germany
Italy
Germany
France

I want to create a new column

df['status']

and assign it values based on the code column. Wherever, the code is Germany, I want to assign df['status'] the value "ON". Everywhere else, I want to write "OFF". How can I achieve this?

I some solutions using .apply()but I read that there are slower, which is why I am looking for alternatives.

Jnl
  • 194
  • 1
  • 11

1 Answers1

1

You can use masking:

# Create column with default value
df["status"] = "OFF"

# Change value based on mask value
df.loc[df["code"] == "Germany", "status"] = "ON"

And I would use True and False instead of "ON" and "OFF", it saves some memory and it is a generally better code practice.

Benjamin Breton
  • 1,388
  • 1
  • 13
  • 42