循环在一个中创建多个数据范围
我正在尝试制作一个循环,该循环将从一个大数据框架中输出多个数据范围。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其检查每个唯一项目,不如在关注的变量上进行
.groupby
: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:The
group_df
is the same df one would get withraw_df.loc[raw_df['name'] == name]
.你可以做
You could do