Skip to content
Advertisement

Filter pandas dataframe column and replace values using a list condition

I have the following dataframe:

JavaScript

And I have this list of possible acceptable values for everyone that has a type of Contingent Workers:

JavaScript

I need to find a way to confirm if everyone under the type “Contingent Worker” have an accetpable value in “Job” and, if not (or blank value), replace that value for “Consultant” resulting in this dataframe:

JavaScript

What would be the best way to achieve this result?

Advertisement

Answer

I would do it following way

JavaScript

gives output

JavaScript

Explanation: select such rows where Type is Contingent Worker and (&) Job is not (~) one of values (isin) from your list, select Job column, set value to Consultant.

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