Home > Enterprise >  Changing the value of column based on conditions: Pandas
Changing the value of column based on conditions: Pandas

Time:12-25

I have the following information.

Let's say I have the following list.

my_list = [2,3,4,5]

My dataframe is as follows:

df

Col1      Value
[2,3,6]    Hot
[7]        Mild
[10,11]    Cool
[5,9]      Cool
[2,5,6]    Mild

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 Mild in the corresponding row. But if the Value is Hot keep as it is.

I would like to see something like below.

Col1      Value
[2,3,6]    Hot
[7]        Mild
[10,11]    Cool
[5,9]      Mild
[2,5,6]    Mild

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.

I have tried this.

df["Value"] =(d["Value"].apply(lambda x: "Mild" if  len(set(x["Col1"]).intersection(my_list)) >0 
                                            and (x["Value"] != 'Hot') else x["Value"],axis=1) )                           

Can any one help on this?

CodePudding user response:

The approach is almost identical to your previous question, just change one condition:

match = df['Col1'].explode().isin(my_list).groupby(level=0).any()
df['Value'] = df['Value'].mask(match & df['Value'].ne('Hot'), 'Mild')

output:

        Col1 Value
0  [2, 3, 6]   Hot
1        [7]  Mild
2   [10, 11]  Cool
3     [5, 9]  Mild
4  [2, 5, 6]  Mild

CodePudding user response:

Use mask to hide rows with Hot value then explode your dataframe before check Col1 values are in my_list:

df.loc[df.mask(df['Value'].eq('Hot')).explode('Col1').isin(my_list)
         .groupby(level=0)['Col1'].any(), 'Value'] = 'Mild'
print(df)

# Output:
        Col1 Value
0  [2, 3, 6]   Hot
1        [7]  Mild
2   [10, 11]  Cool
3     [5, 9]  Mild
4  [2, 5, 6]  Mild

CodePudding user response:

We can use set.isdisjoint to check if the lists are disjoint then use boolean indexing with loc to update the values

m1 = df['Value'] != 'Hot'
m2 = df['Col1'].map(set(my_list).isdisjoint)

df.loc[m1 & ~m2, 'Value'] = 'Mild'

        Col1 Value
0  [2, 3, 6]   Hot
1        [7]  Mild
2   [10, 11]  Cool
3     [5, 9]  Mild
4  [2, 5, 6]  Mild
  • Related