oceanbeach96
oceanbeach96

Reputation: 634

Looping through multiple DataFrames to perform same task

I run the following code and get the desired output for one DataFrame, a:

a = a.reset_index()
a['count'] = 1
a = pd.DataFrame(a.groupby(['country','id','town','date'])['count'].mean())
a = a.groupby(['date','town']).count()
a['percentage'] = a['count'].div(a.groupby('date').['count'].transform('sum')).mul(100)
a = a['percentage'].unstack()

However, I have multiple DataFrames (a,b,c,d,e,f,g,h) and am not sure how to loop through them all. Any help to save me manually doing it would be awesome!

Upvotes: 0

Views: 195

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14093

I guess one option is to use dictionaries with a function:

# sample data
a = pd.DataFrame(np.random.randn(5,5))
b = pd.DataFrame(np.random.randn(5,5))
c = pd.DataFrame(np.random.randn(5,5))

# create a dict with a key as the "variable name"
dfs = {'a':a, 'b':b, 'c':c}

# some fucntion
def myFunc(df):
    # do stuff
    return df.sum().to_frame()

# dict comprehension
d = {k:myFunc(v) for k,v in dfs.items()}

# call dataframes with the key
d['a']

              0
    0  2.023154
    1 -0.598737
    2 -0.879587
    3 -3.264965
    4  0.974626

Upvotes: 1

Related Questions