更新数据框(dataframe)如果满足特定条件

huangapple go评论98阅读模式
英文:

Update dataframe if certain condition is met

问题

以下是翻译好的代码部分:

  1. df['Column5'] = np.where(df['Column6'] == "TEST", df['Column5'] + 1, df['Column5'])

请注意,此代码将检查在"Column6"列中是否存在"TEST"条件,如果是,则将"Column5"中的值加1。

英文:

Any help would be greatly appreciated here:

Lets say I have a Pandas DataFrame such as:

  1. Column1 Column2 Column3 Column4 Column5 Column6
  2. 0 aaa abb jnhs 01/01/2020 40 TEST
  3. 1 aba vvv jnjh 01/01/2020 34 TEST

I am looking for the best way to be able to test if a certain condition exists, if it does to take
the number on the same row under the heading "column5" below and add 1 to that number. Then to return the full dataframe with just 1 added to the cells in col5 where col6 ="TEST".

The below is the Dataframe result I am looking for:

  1. Column1 Column2 Column3 Column4 Column5 Column6
  2. 0 aaa abb jnhs 01/01/2020 41 TEST
  3. 1 aba vvv jnjh 01/01/2020 35 TEST

What I have tried so far:

df['Column5'] = np.where(df['Colum6'] == "TEST", +1, "NA")

This howevever replaces the value in column5 with 1 and does not add 1 to the current value to make them 41 and 35 respectively.

Thank you for any help.

答案1

得分: 2

你可以查找满足条件的行,然后在 Column5 上加1。

  1. mask = df["Column6"] == "TEST"
  2. df.loc[mask, "Column5"] += 1
英文:

You can look for the rows where the condition is met, then add +1 to Column5

  1. mask = df["Column6"] == "TEST"
  2. df.loc[mask, "Column5"] += 1

答案2

得分: 1

df['Column5'] = np.where(df['Colum6'] == "TEST", df['Column5'] + 1, "NA")

英文:

This work:

  1. df['Column5'] = np.where(df['Colum6'] == "TEST", df['Column5']+1, "NA")

答案3

得分: 1

你可以执行以下操作:

  1. m = df["Column6"].eq("TEST")
  2. df.loc[m, "Column5"] = df.loc[m, "Column5"].add(1)
英文:

You can do:

  1. m = df["Column6"].eq("TEST")
  2. df.loc[m, "Column5"] = df.loc[m, "Column5"].add(1)

答案4

得分: 1

以下是翻译好的部分:

The pandas documentation recommends using a mask to index the desired rows. And then use the += operator as suggested in another answer or index any other row and just add +1 or any other desired value.

  1. import pandas as pd
  2. import numpy as np
  3. data = {"Index": [0,1, 2], "A": [1, 1, 1],
  4. "B": [42, 42, 42], "C": ["test","test", "NA"]}
  5. df = pd.DataFrame(data)
  1. >>>df
  2. Index A B C
  3. 0 0 1 42 test
  4. 1 1 1 42 test
  5. 2 2 1 42 NA
  1. mask = df["C"]=="test";
  2. df.loc[mask, "B"] = df["B"]+1
  1. >>> df
  2. Index A B C
  3. 0 0 1 43 test
  4. 1 1 1 43 test
  5. 2 2 1 42 NA
英文:

The pandas documentation recommends using a mask to index the desired rows. And then use the += operator as suggested in another answer or index any other row and just add +1 or any other desired value.

  1. import pandas as pd
  2. import numpy as np
  3. data = {"Index" : [0,1, 2], "A": [1, 1, 1],
  4. "B": [42, 42, 42], "C": ["test","test", "NA"]}
  5. df = pd.DataFrame(data)
  1. >>>df
  2. Index A B C
  3. 0 0 1 42 test
  4. 1 1 1 42 test
  5. 2 2 1 42 NA
  1. mask = df["C"]=="test"
  2. df.loc[mask, "B"] = df["B"]+1
  1. >>> df
  2. Index A B C
  3. 0 0 1 43 test
  4. 1 1 1 43 test
  5. 2 2 1 42 NA

答案5

得分: 0

尝试这样做:

  1. import pandas as pd
  2. result = df["column6"] == "TEST"
  3. df.loc[result, "column5"] = df["column5"] + 1
英文:

Try this:

  1. import pandas as pd
  2. result=df["column6"]=="TEST"
  3. df.loc[result,"column5"]=df["column5"]+1

huangapple
  • 本文由 发表于 2023年2月24日 01:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定