循环:结果通过+ =更新,如果在同一日期,否则写下一行 [英] Loop: Results to be updated via += if on the same date, otherwise write next line

查看:107
本文介绍了循环:结果通过+ =更新,如果在同一日期,否则写下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产生这样的输出的函数。

 日期现金流计数等
1/1/2000 40 3 ...

总是一行代码。这是多次创建的,具有相同或不同的日期。现在我想记录所有结果

 日期现金流计数等
1/1/2000 40 3 .. 。

所以如果一个新的结果出现在相同的日期/索引中,它应该添加和更新该字段即通过+ =,但是如果它的表中不存在不同的日期,那么它应该只是附加并为进一步的更新提供基础,即

新结果

 日期现金流量等
1/1/2000 -20 1 ...

然后我的表格应显示

 日期现金流量等
1/1/2000 20 4 ...

然而,如果新的结果在日期中还没有存在的日期点击

 日期现金流计数等
2/1/2000 10 20 ...

然后我的表看起来就像

 日期现金流计数等
1/1/2000 20 4 ...
2/1/2000 10 20 ...



编辑1:看起来我的问题没有明确表达出来:程序如何看起来像这样:

1)如果索引重合:update values + =

/ p>

2)如果index不存在,则按该行扩展表

edit2:我该如何需要改变下面的代码,如果我的日期被设置为索引?

解决方案

您可以使用 DataFrame.append ,然后按列你想用作索引。



假设 total_df 是要添加新的表格行, new_df 是包含这些新行的表,并且 date 是要用作索引的列。然后你可以使用:

  total_df.append(new_df).groupby(by ='date')。sum()


I have a function that produces some output like this

date         cashflow       count   etc
1/1/2000        40            3       ...

always a one-liner. This is created many times, with same or different dates. Now I would like a record of all results

date      cashflow         count    etc
1/1/2000     40              3      ...

so if a new result comes in with the same date / index, than it should add and update the field, i.e. via "+=", however if its a different date that does not already exists in the table, than it should just append and provide basis for further "updates", i.e.

new result

date      cashflow         count    etc
1/1/2000     -20              1      ...

then my table should show

date      cashflow         count    etc
1/1/2000     20              4      ...

however if a new results hits in with a date not yet existing in the table

date      cashflow         count    etc
2/1/2000     10              20     ...

then my table would look like

date      cashflow         count    etc
1/1/2000     20              4      ...
2/1/2000     10              20     ...

edit1: It seems my question did not come out clear: How would a program look like that could:

1) if index coincide: update values +=

2) if index not does not exist, extend the table by that line

edit2: How would I need to change the below code, if my date was set as an index?

解决方案

You can use DataFrame.append and then group by the column you want to use as an index.

Let's say that total_df is the table to which you want to add new rows, new_df is the table containing those new rows and date is the column to be used as index. Then you can use:

total_df.append(new_df).groupby(by='date').sum()

这篇关于循环:结果通过+ =更新,如果在同一日期,否则写下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆