【问题标题】:Looping through multiple DataFrames to perform same task循环多个 DataFrame 以执行相同的任务
【发布时间】:2019-03-28 01:26:09
【问题描述】:

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

但是,我有多个 DataFrames (a,b,c,d,e,f,g,h) 并且不知道如何循环商场。任何帮助我手动操作都会很棒!

【问题讨论】:

    标签: python pandas loops dataframe


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 2011-07-16
      • 2014-03-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多