循环在一个中创建多个数据范围

发布于 2025-02-06 12:57:44 字数 475 浏览 1 评论 0原文

我正在尝试制作一个循环,该循环将从一个大数据框架中输出多个数据范围。

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

I am trying to make a loop that will output into multiple dataframes from one large dataframe.

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()

Expected outcome for all names: joe.head()

name   id   sum
joe    10   100    
joe    12   110

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2025-02-13 12:57:44

与其检查每个唯一项目,不如在关注的变量上进行.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相同的DF。 [raw_df ['name'] ==名称]

Instead of checking for each unique item, it's possible to do .groupby on the variable of interest:

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()

The group_df is the same df one would get with raw_df.loc[raw_df['name'] == name].

要走干脆点 2025-02-13 12:57:44

你可以做

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

You could do

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文