pandas 有没有办法计算一行中两个值的总和并更新下一行的结果
我有一个数据框 df ,如下所示:
A B C R1 R2
2 2 5 Nan Nan
1 1 8 7 Nan
5 5 10 Nan Nan
1 1 14 Nan Nan
我正在尝试更新数据框,在其中添加 B 和 C 并将结果写入 R1 中,方法是将其偏移 1 行。例如,2+5 = 7(偏移一行)并将结果存储在 R1 中。然后我将此结果与 C 列 (8) 的相应值进行比较。如果 C>R1,则将 C 和 A 相加并存储在 R1 中,偏移量为 1。如果 C
A B C R1 R2
2 2 5 Nan Nan
1 1 8 7 Nan
5 5 10 9 Nan
1 1 14 15 Nan
Nan Nan Nan Nan 15
Is there a way in pandas这样做吗?
I have a data frame df that looks like this:
A B C R1 R2
2 2 5 Nan Nan
1 1 8 7 Nan
5 5 10 Nan Nan
1 1 14 Nan Nan
I am trying to update the dataframe where I add B and C and write the result in R1 by offsetting it by 1 row. For example, 2+5 = 7 (offset by a row) and store the result in R1. Then I compare this result with the corresponding value of column C (8). If C>R1, Add C and A and store in R1 by offsetting by 1. If C<R1, add C and B and store in R2 by offsetting by 1. The result should be as like this -
A B C R1 R2
2 2 5 Nan Nan
1 1 8 7 Nan
5 5 10 9 Nan
1 1 14 15 Nan
Nan Nan Nan Nan 15
Is there a way in pandas to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了呈现一个更具指导性的示例,我使用了一个 DataFrame
A 和 B 中的值不同,并且最初没有 R1 和 R2
columns:
要添加 R1 列,您可以运行:
到目前为止的结果是:
我注意到到目前为止您不需要任何额外的行,
最后一行的 B + C 结果。
下一步是添加一行填充 NaN:
这需要提供空间来保存(下移)结果
从最后一个原始行开始。
结果是:
下一步(如果 C>R1)可以执行为:
结果是:
生成 R2 列我们可以从创建空列开始:
然后我们保存实际值:
结果是:
请注意,R2 包含两个 非 NaN 值,因为我的源
数据与你的略有不同。
最后一步是有条件地删除最后一行,
如果它仍然包含所有NaN:
对于当前源数据没有任何变化。
To present a more instructive example, I took a DataFrame with
different values in A and B, and initially without R1 and R2
columns:
To add R1 column, you can run:
The result so far is:
I noticed that so far you don't want any additional row,
resulting from B + C for the last row.
The next step is to add a row filled with NaN:
This is needed to provide the space to save the (shifted down) result
from the last original row.
The result is:
The next step (If C>R1) can be done as:
The result is:
Generation of R2 column we can start from creation of an empty column:
Then we save there actual values:
The result is:
Note that R2 contains two non-NaN values, since my source
data is slightly different to yours.
And the last step is to conditionally drop the last row,
if it still contained all NaNs:
For the current source data nothing changes.
最好通过将代码放入问题中来向 SO 用户展示您的努力。无论如何,您可以检查一下:
这不是干净的代码,也不够简单,但这可以给您解决问题的想法。然后你就可以写出更好的形状。如果我调用 df,结果将是:
It's better to show your effort to SO users by putting your code in the question. Anyway, You can check this:
It's not clean code and not straightforward enough, but this can give you the idea to solve the issue. Then you can write it in better shape. and if I call
df
, the result will be: