'assign a value based on another column's value [duplicate]
I have a dataset df
that 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.
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Benjamin Breton |