[英]How to compare two columns in DataFrame and change value of third column based on that comparison?
I have following table in Pandas:我在 Pandas 中有下表:
index | project | category | period | update | amount
0 | 100130 | labour | 202201 | 202203 | 1000
1 | 100130 | labour | 202202 | 202203 | 1000
2 | 100130 | labour | 202203 | 202203 | 1000
3 | 100130 | labour | 202204 | 202203 | 1000
4 | 100130 | labour | 202205 | 202203 | 1000
And my final goal is to get table grouped by project and category with summary of amount column but only from month of update until now.我的最终目标是让表格按项目和类别分组,并包含金额列的摘要,但仅限于从更新月份到现在。 So for example above I will get summary from 202203 until 202205 which is 3000 for project 100130 and category labour.因此,例如上面的例子,我将获得从 202203 到 202205 的摘要,对于项目 100130 和类别劳动力,这是 3000。
As a first step I tried following condition:作为第一步,我尝试了以下条件:
for index, row in table.iterrows():
if row["period"] < row["update"]
row["amount"] = 0
But:但:
table["amount"] = 0 if table["period"] < table["update"] else None
I did some more research and this code seems to solve my problem:我做了更多研究,这段代码似乎解决了我的问题:
def check_update(row):
if row["period"] < row["update"]:
return 0
else:
return row["amount"]
table["amount2"] = table.apply(check_update, axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.