简体   繁体   English

如何比较 DataFrame 中的两列并根据该比较更改第三列的值?

[英]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:但:

  1. this iteration is not working此迭代不起作用
  2. is there some simple and not so time consuming way how to do it?有没有一些简单又不那么耗时的方法呢? As my table has over 60.000 rows, so iteration not so good idea probably.因为我的表有超过 60.000 行,所以迭代可能不是一个好主意。
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.

相关问题 如何比较两列并从第三列返回值 Pandas dataframe - How to compare two columns and return value from a third column in Pandas dataframe 如何比较两个 dataframe 列并将第三列值提取为 python 中的 output - How to compare two dataframe columns and extract third column value as output in python 根据 dataframe 中的其他两列编辑第三列的值 - editing value of third column based on other two columns in dataframe 循环列以一次比较数据框中的两列,并根据每个比较结果添加一个汇总列 - Loop over columns to compare two columns at a time in a dataframe and add one summary column based on each comparison result 比较无序 DataFrame 基于比较比较值并创建一个新列 - Compare unordered DataFrame compare value based on comparison and create a new column 如何比较 dataframe 中的两列并根据匹配字段更新列 - how to compare two columns in dataframe and update a column based on matching fields Dataframe - 对于每一行,比较两列的值,匹配时获取第三列的值 - Dataframe - for each row, compare values of two columns, get value of third column on match 比较 pandas DataFrame 中的两个日期列以验证第三列 - Compare two date columns in pandas DataFrame to validate third column 比较两列中的值并提取 dataframe 中第三列的值 - Compare the values in two columns and extract the values of a third column in a dataframe 如何遍历熊猫数据框并基于第三列比较某些列? - How to iterate over a pandas dataframe and compare certain columns based on a third column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM