mardi 3 novembre 2020

Assign the random value based on satisfied condition accordingly to the matching index of pandas dataframe column

I am trying to populate data in python.

The code is below.

The intention of the code is to choose a random value in the range and populate the panda's variable according to the size of dataframe column . To Achieve the solution I define index and item variable in the for loop if the condition is met, then add the random value at the index of the looped variable in a new variable position

import scipy.stats as stats
for index , item in df['RangeCategory']:
    if (item ==  'RC1' ):
        upperLim = 0.5
        lowerLim = 50.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC2' ):
        upperLim = 50
        lowerLim = 250.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC3' ):
        upperLim = 250
        lowerLim = 300.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC4' ):
        upperLim = 300
        lowerLim = 500.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC5' ):
        upperLim = 500
        lowerLim = 900.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC6' ):
        upperLim = 900
        lowerLim = 1500.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC7' ):
        upperLim = 1500
        lowerLim = 3000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC8' ):
        upperLim = 3000
        lowerLim = 5000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC9' ):
        upperLim = 5001
        lowerLim = 9000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC10' ):
        upperLim = 9001
        lowerLim = 10000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC11' ):
        upperLim = 10001
        lowerLim = 30000
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC12' ):
        upperLim = 31000
        lowerLim = 50000
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)

Dataframe has 100K Data. Current Problem:"Too many values to unpack"

I came across the issue "Too many values to unpack". can you please guide me here on a correction?

Expected Result: Assign the random value based on satisfied condition accordingly

Best Regards, Gabe




Aucun commentaire:

Enregistrer un commentaire