Home > Net >  pandas iteration for loop
pandas iteration for loop

Time:09-01

def calculate(x, formula):
    return eval(formula)

col = {
    's1':{'l':100, 'w':200}, 
    's2':{'l':200, 'w':400}, 
    's3':{'l':300, 'w':500}
    }

coldf = pd.DataFrame.from_dict(col, orient='index')

print(coldf)

formulacol = [{'key':'a', 'formula':"x['l']*x['w']"},{'key':'p', 'formula':"x['l'] x['w'] x['l'] x['w']"}]

for i in formulacol:
    coldf[i['key']] = coldf.apply(lambda x : calculate(x, i['formula']), axis=1)

print(coldf)

i have a for loop to iterate the formula list and apply on dataframe. is there anyway to avoid the for loop.

CodePudding user response:

Instead of formulacol, define a dictionary with column names as keys and respective expressions as values:

dct = {'a': coldf.l * coldf.w, 'p': coldf.l   coldf.w   coldf.l   coldf.w}

Then call assign with dictionary unpacking and save the result back under the original DataFrame:

coldf = coldf.assign(**dct)

The result is:

      l    w       a     p
s1  100  200   20000   600
s2  200  400   80000  1200
s3  300  500  150000  1600

CodePudding user response:

I'm not sure I understand correctly your question but maybe something like this is what you are looking for?

import pandas as pd

col = {
    's1':{'l':100, 'w':200}, 
    's2':{'l':200, 'w':400}, 
    's3':{'l':300, 'w':500}
    }

coldf = pd.DataFrame.from_dict(col, orient='index')

print(coldf)

formulacol = pd.DataFrame(
    {
        'a' : coldf['l'] * coldf['w'], 
        'p' : coldf['l']   coldf['w']   coldf['l']   coldf['w']
    }
)

coldf = pd.concat([coldf, formulacol], axis=1)

print(coldf)
  • Related