我运行以下代码,并为一个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()
但是,我有多个数据帧(a
,b
,c
,d
,e
,f
,{{1} },g
),并且不确定如何遍历它们。任何帮助我手动保存的帮助都很棒!
答案 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