Skip to content
Advertisement

Filling column based on conditions

In the DataFrame below

JavaScript

I am trying to update the Value column based on the following conditions, if Action is Sell check if the Status is not - if both the conditions are true then the first two characters of the Country needs to be updated as the Value column else if Status column is - and the Action column is Sell the Value column needs to be updated with the Name column without the characters Ve_, if Action is not Sell leave the Value column as np.NaN

But the output I am expecting is

JavaScript

I have tried with np.where and df.loc both didn’t work. Please do help me because I am out of options now

What I have tried so far is

JavaScript

but I am getting the output as <pandas.core.strings.StringMethods object at 0x000001EDB8F662B0> wherever Iam trying to extract substrings so the output looks like this

JavaScript

Advertisement

Answer

You have two conditions ,we can do it with np.select

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement