EarlofMar
EarlofMar

Reputation: 126

Multiplying a specific cell value based on if statement in a Pandas dataframe

So I am trying to associate a specific value ('Property Damage') with every row in my dataset but I am having some trouble with this. Specifically, I want to multiply the value in the 'MD' column for each row by a number (0.02, 0.15, etc.) if it meets the conditions specified in the for loop (e.g. if i >= 0.8062, print etc.). I have included my code below:

df['RAND'] = np.random.uniform(0, 1, size=df.index.size)
dfRAND = list(df['RAND'])

def sim_1():
for i in dfRAND:
    result = []
    if i >= 0.8062:
        df['Property Damage'] = df['MD'].apply(lambda x: x * 0.02)
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)), 'LF0', i,':', df['Property Damage'])
    elif 0.01 <= i < 0.89062:
        df['Property Damage'] = list(df['MD'].apply(lambda x: x * 0.15))
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)),'LF1', i, ':', df['Property Damage'])
    elif 0.05 <= i < 0.01:
        df['Property Damage'] = list(df['MD'].apply(lambda x: x * 0.20))
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)),'LF2', i,':', df['Property Damage'])
    elif 0.025 <= i < 0.05:
        df['Property Damage'] = list(df['MD'].apply(lambda x: x * 0.50))
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)),'LF3', i,':', df['Property Damage'])
    elif 0.0125 <= i < 0.025:
        df['Property Damage'] = list(df['MD'].apply(lambda x: x * 1))
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)),'LF4', i,':', df['Property Damage'])
    elif 0.0063 <= i < 0.0125:
        df['Property Damage'] = list(df['MD'].apply(lambda x: x * 1))
        print(list(val for x, val in enumerate(df['Count']) if 
                   x == dfRAND.index(i)),'LF5', i,':', df['Property Damage'])

The problem I am having at the moment is that the code prints all the 'Property Damage' values for each row. I want it to give me the 'Property Damage' value for a specific row based on whichever condition is met in the for loop.

Any help is appreciated. Thanks in advance.

Upvotes: 0

Views: 503

Answers (1)

Brian
Brian

Reputation: 1604

Are you looking for something like this?

my_bins = {pd.Series.max(df['RAND'])-1: 1, 
           .01: .15, 
           .0125: 1, 
           .025: .5, 
           .05: .2,
           pd.Series.max(df['RAND'])+1 : .02}
df['rand_multiplier'] = pd.cut(df['RAND'], bins = sorted(my_bins.keys()), labels = list(range(len(my_bins) - 1))).apply(lambda x: my_bins[sorted(my_bins.keys())[x]])

df.apply(lambda row: row['MD'] * row['rand_multiplier'], axis = 1)

I'm in a bit of a hurry so it's not the prettiest thing. Basically I created bins based on the criteria you had and created a "multiplier" column which associates each entry in df['RAND'] with a multiplying factor. Then we can iterate over df and apply the multiplying factor to your 'MD' row.

Of course, I can't show the produced results without the 'MD' data.

Upvotes: 1

Related Questions