如何根据条件和直至0

发布于 2025-01-26 00:20:03 字数 465 浏览 2 评论 0 原文

我是Python和Pandas的新手,所以这是我的问题:

我有两个数据范围,DF1有两个用于标签的列,一个用于整数,而与每个标签的toal相对应,而DF2包含白天使用的数量。我想减去DF2的每一行,直到DF1等于或接近0,然后将列添加到DF1的最后一行的日期(可以在新的DataFrame DF3中)。减法需要有两个条件:

  1. 标签需要相等的
  2. 减法不能少量到0

df1 = df总计

df2 = df的数量

我希望有人可以帮助我。

I'm new with python and pandas so here's my question:

I have two dataframes, df1 has two columns one for labels and one for integers which correspond to the Toal of each label while df2 contains the quantity used by day. I would like to subtract each row of df2 until df1 is equal or closer to 0 and add a column to df1 the date of the last row subtracted (it could be in a new dataframe df3). The subtraction needs to have two conditions where:

  1. labels need to be equal
  2. subtraction can not be minor to 0

df1 = DF with Totals

df2 = DF with quantities

I hope someone can help me.

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

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

发布评论

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

评论(1

忆离笙 2025-02-02 00:20:03

我相信.cumsum()和.idxmin()将有所帮助。

  1. 在标签上加入您的dataframes,
  2. 创建一个新的“运行数量”列,该列是“数量”列上的.cumsum()(; 在.cumsum())上的博客文章
  3. 创建一个新的“运行总计”列,即“ total” - “运行数量”
  4. 过滤器,仅在“运行总计”中的正值( stackoverflow答案有关过滤负值
  5. 过滤到每个标签的“运行总计”的最小值。 ://pandas.pydata.org/docs/reference/api/pandas.dataframe.idxmin.html“ rel =” nofollow noreferrer“ /stackoverflow.com/a/59343150/5618434"> stackoverflow关于.idxmin())

这应该为您提供一个三列数据框架,每个标签一行,运行总计的日期最接近,但是不低于0,金额(该日期总数 - 运行量)。

I believe a .cumsum() and .idxmin() will help with this.

  1. Join your dataframes on label
  2. Create a new "Running Quantity" column that is a .cumsum() on the "Quantity" column (pandas documenation on .cumsum(); blog post on .cumsum())
  3. Create a new "Running Total" column that is "Total" - "Running Quantity"
  4. Filter to only the positive values in "Running Total" (StackOverflow answer about filtering out negative values)
  5. Filter to the minimum value of "Running Total" per label using .idxmin() (pandas documentation on .idxmin(); StackOverflow answer about .idxmin())

This should give you a three-column data frame with one row per label, the date when the running total was closest to but not lower than 0, and the amount (Total - Running Quantity at that date).

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