Skip to content
Advertisement

Check if a value exist in a column and change in another Pandas

I have the following information.

Let’s say I have the following list.

JavaScript

My dataframe is as follows:

df

JavaScript

I would like to check if one of the value from the list my_list exist in the column Col1. If it exist, change the value in Value column to Hot in the corresponding row.

I would like to see something like below.

JavaScript

I am just looking for a simple script that can iterate and check in every row and change a value in another column the corresponding row.

Can any one help on this?

Advertisement

Answer

You can use a combination of explode, isin, agg, and .loc:

JavaScript

Output:

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