【问题标题】:loop to create multiple dataframes out of one循环以从一个数据帧中创建多个数据帧
【发布时间】:2022-08-06 12:21:17
【问题描述】:

我正在尝试创建一个循环,该循环将从一个大数据帧输出到多个数据帧。

raw_df[\'names\'] = [joe, joe, bob, john, john]
raw_df[\'order_id\'] = [10, 12, 5, 20, 25]
raw_df[\'amount\'] = [100, 1000, 200, 20 25]

for name in raw_df[\'name\'].unique():
    names = pd.DataFrame(raw_df.loc[raw_df[\'name\'] == name])
    name[\'cummulative_sum\'] = owner_names[\'amount\'].cumsum()

所有名称的预期结果:joe.head()

name   id   sum
joe    10   100    
joe    12   110

    标签: python pandas dataframe group-by


    【解决方案1】:

    可以对感兴趣的变量执行.groupby,而不是检查每个唯一项目:

    for group_name, group_df in raw_df.groupby("name"):
       print("Processing name:", group_name)
       names = group_df # this is the same as "names" in your snippet
       names["cum_sum"] = names["amount"].cumsum()
    

    group_df 与 raw_df.loc[raw_df['name'] == name] 得到的 df 相同。

    【讨论】:

      【解决方案2】:

      你可以做

      variables = locals()
      
      for name, data in raw_df.groupby('names'):
          variables[name] = data
       
      joe
      Out[607]: 
        names  order_id  amount
      0   joe        10     100
      1   joe        12    1000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        • 2019-07-29
        相关资源
        最近更新 更多