遍历多个DataFrame以执行相同任务

时间:2019-03-28 01:26:10

标签: python pandas loops dataframe

我运行以下代码,并为一个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()

但是,我有多个数据帧(abcdef,{{1} },g),并且不确定如何遍历它们。任何帮助我手动保存的帮助都很棒!

1 个答案:

答案 0 :(得分:1)

我猜一个选择是使用带有功能的字典:

# 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