Skip to content
Advertisement

Write a code to perform the same operation to multiple pandas DataFrames

I am trying to write a loop/definition to perform the same operation to multiple panda DataFrames. My aim is to get 5 pandas DataFrames with names a, b, c, d and e and to multiple of operations to them. What i get is “NameError: name ‘a’ is not defined”, and the new files are not written.

I just can’t get it to work. There are many treads in how to import multiple files into one single DataFrame, but can’t find anything on this issue. Many thanks for your time and help!!

This is an example of what i have tried so far:

list = ['a', 'b', 'c', 'd', 'e']

for i in list:
    i = pd.DataFrame()
    i = pd.read_csv(i + 'csv')
    i['sum'] = i['Z'] + i['Y']
    i.to_csv(i + 'new.csv')

a.info() 

Advertisement

Answer

You would do it using a dict:

list = ['a', 'b', 'c', 'd', 'e']
d = {l: pd.DataFrame() for l in list}

for k in d:
    d[k] = pd.read_csv(k + '.csv')
    d[k]['sum'] = d[k]['Z'] + d[k]['Y']
    d[k].to_csv(d[k] + 'new.csv')

d['a'].info()

That way you can acess your pd.DataFrames by the name (string).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement