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

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

Update dataframe if certain condition is met

问题

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

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:

Column1 Column2 Column3 Column4  Column5 Column6

0  aaa      abb  jnhs  01/01/2020      40   TEST
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:

Column1 Column2 Column3 Column4  Column5 Column6

0  aaa      abb  jnhs  01/01/2020      41   TEST
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。

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

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

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

答案2

得分: 1

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

英文:

This work:

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

答案3

得分: 1

你可以执行以下操作:

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

You can do:

m = df["Column6"].eq("TEST")
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.

import pandas as pd
import numpy as np

data = {"Index": [0,1, 2], "A": [1, 1, 1],
        "B": [42, 42, 42], "C": ["test","test", "NA"]}

df = pd.DataFrame(data)
>>>df

Index  A  B  C
0  0  1  42  test
1  1  1  42  test
2  2  1  42  NA
mask = df["C"]=="test";

df.loc[mask, "B"] = df["B"]+1
>>> df

Index  A  B  C
0  0  1  43  test
1  1  1  43  test
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.

import pandas as pd
import numpy as np

data = {"Index" : [0,1, 2], "A": [1, 1, 1],
        "B": [42, 42, 42], "C": ["test","test", "NA"]}

df = pd.DataFrame(data)
>>>df

Index 	A 	B 	C
0 	0 	1 	42 	test
1 	1 	1 	42 	test
2 	2 	1 	42 	NA
mask = df["C"]=="test"

df.loc[mask, "B"] = df["B"]+1
>>> df

Index 	A 	B 	C
0 	0 	1 	43 	test
1 	1 	1 	43 	test
2 	2 	1 	42 	NA

答案5

得分: 0

尝试这样做:

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

Try this:

import pandas as pd
result=df["column6"]=="TEST"
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:

确定