Home > Software engineering >  How do i set more than 1 value if a condition is met?
How do i set more than 1 value if a condition is met?

Time:05-05

i wonder if its possible to set 3 values if condition is met: for exemple i have a 10000 row file in excel , and i want to try a code to automate this process because i can't do it manually , i have a lot of blank cells in column Rate and i want to fill it with a predefined rates between (0%,0-20%,20-40%,40-60%)

Company         Interest     Rate
X1        not interested     0%
X2        interested         0-20%
X3        interested         20-40%
X4        interested         40-60%
X5        not interested     0%
X6        interested         
X7        interested         
X8        interested     

So i want to apply a if condition where the rate of companies interested is between these three options (0-20%,20-40%,40-60%) to fill the Rate column

What i've tried ?

so its more like this function if its existed :

df.loc[df['Interest'] == 'interested', 'Rate'] ="0-20%" OR "40-60%" OR "20-40%"

Desired Results

Company         Interest     Rate
X1        not interested     0%
X2        interested         0-20%
X3        interested         20-40%
X4        interested         40-60%
X5        not interested     0%
X6        interested         0-20%   
X7        interested         40-60%
X8        interested         20-40%

CodePudding user response:

You can make use of the apply function to alter existing dataframe.

import random
import pandas as pd

df = pd.DataFrame({ 'Interest' : ['interested','interested','interested']})
df['Rate'] = "dummy"

df.Rate = df.apply(lambda x: random.choice(["0-20%", "40-60%", "20-40%"]) if x.Interest == 'interested' else x.Rate, axis=1)

print(df)

CodePudding user response:

You could use random.choice for that.

edited my answer like in the comments asked.

import random

rndm = ["0-20%", "40-60%", "20-40%"]
df['Rate'] = df['Rate'].fillna(value=-1)
df['Rate'] = [random.choice(rndm) if (x=='interested' and y==-1) else '0%' if x=='not interested' else y for x,y in zip(df['Interest'],df['Rate'])]

Here is the other (and better) solution modified for your purpose of only filling part of the column:

rndm = ["0-20%", "40-60%", "20-40%"]
df['Rate'] = df['Rate'].fillna(value=-1)
df['Rate'] = df.apply(lambda x: random.choice(rndm) if (x['Interest'] == 'interested' and x['Rate']==-1) else x['Rate'], axis=1)
  • Related