简体   繁体   English

PANDAS:根据多个if条件为某列的每一行赋值

[英]PANDAS : Assigning a value for each row in certain column based on multiple if condition

I'm really new to python codes and having trouble with applying some of the answers for similar questions for my own case.我真的是 python 代码的新手,并且在为我自己的案例应用类似问题的一些答案时遇到了麻烦。 Please help请帮忙

So I have a dataframe with Column A and B, with numerous rows所以我有一个带有 A 列和 B 列的 dataframe,有很多行

Both contains negative and positive numbers, and I'm trying to make a new Column C with following conditions两者都包含负数和正数,我正在尝试使用以下条件创建一个新的列 C

If "Value of row 1, Column A is less than 0" & "Value of row 1, Column B is less than 0", return -100 in "row 1, Column C"如果“第 1 行 A 列的值小于 0”&“第 1 行 B 列的值小于 0”,则在“第 1 行 C 列”中返回 -100

Elif "Value of row 1, Column A is less than 0" & "Value of row 1, Column B is greater than 0", return 100 in "row 1, Column C" elif "第1行A列的值小于0" & "第1行B列的值大于0", 在"第1行C列"中返回100

Elif "Value of row 1, Column A is greater than 0" & "Value of row 1, Column B is less than 0", return 100 in "row 1, Column C" elif "第1行A列的值大于0" & "第1行B列的值小于0", 在"第1行C列"中返回100

Else: return (Column A.Value / Column B.Value) in Column C否则:在 C 列中返回(A 列值/B 列值)

Thanks a lot非常感谢

I think you are looking for np.select :我认为您正在寻找np.select

condlist = [(df['A'] < 0) & (df['B'] < 0),
            (df['A'] < 0) & (df['B'] > 0),
            (df['A'] > 0) & (df['B'] < 0)]

choicelist = [-100, 100, 100]

default = df['A'] / df['B']

df['C'] = np.select(condlist, choicelist, default)

Output: Output:

>>> df
          A          B           C
0 -0.002639  -1.374507 -100.000000
1 -0.696428   9.923431  100.000000
2  1.410547   3.804043    0.370802
3  1.504908   2.701486    0.557067
4  1.867486   1.889067    0.988576
5 -0.451066 -11.529716 -100.000000
6  5.713800  -7.678271  100.000000
7 -4.318760   5.082725  100.000000
8  5.169819  -4.122461  100.000000
9  0.094524  -1.916718  100.000000

Setup a MRE设置MRE

import pandas as pd
import numpy as np

np.random.seed(2022)
df = pd.DataFrame(np.random.normal(0, 5, (10, 2)), columns=['A', 'B'])
import pandas as pd

# initialize as list
data = [[1, 10], [3, 45], [56, -6], [-96, -65]] 

# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['A', 'B'])
print(df)
print('*****************')

df['C'] = None

if df.iat[0,0] < 0 and df.iat[0,1] < 0:
  df.iat[0,2] = -100

elif df.iat[0,0] < 0 and df.iat[0,1] > 0:
  df.iat[0,2]  = 100

elif df.iat[0,0] > 0 and df.iat[0,1] < 0:
  df.iat[0,2]  = 100

else:
  df['C'] = df['A']/df['B'] 

print('New dataFrame:')
print(df)

OUTPUT: OUTPUT:

    A   B
0   1  10
1   3  45
2  56  -6
3 -96 -65
*****************
New dataFrame:
    A   B         C
0   1  10  0.100000
1   3  45  0.066667
2  56  -6 -9.333333
3 -96 -65  1.476923

Let me know if any confusion!让我知道是否有任何混淆!

One way you could do this is by defining a function with the desired logic and then passing it to the apply function along axis 1 (row-wise).执行此操作的一种方法是定义具有所需逻辑的 function,然后沿轴 1(按行)将其传递给apply程序 function。 In this case, that function might look like the following:在这种情况下,function 可能如下所示:

def f(x):

    if x["A"] < 0 and x["B"] < 0:
        return -100
    elif x["A"] < 0 and x["B"] > 0:
        return 100
    elif x["A"] > 0 and x["B"] < 0:
        return 100
    else:
        return x["A"] / x["B"]

We can then generate some sample data for testing purposes:然后我们可以生成一些样本数据用于测试目的:

>>> import numpy as np
>>> import pandas as pd
>>> data = np.random.randint(-50, 50, size = (10, 2))
>>> df   = pd.DataFrame(data, columns = ["A", "B"])
>>> df
    A   B
0  23   4
1  10  25
2 -14  45
3  31  32
4  49  32
5 -23  34
6 -10 -29
7  10 -19
8 -45 -48
9  31 -31

Finally, we can apply our function to the sample data:最后,我们可以将 function 应用于示例数据:

>>> df["C"] = df.apply(f, axis = 1)
>>> df
    A   B          C
0  23   4    5.75000
1  10  25    0.40000
2 -14  45  100.00000
3  31  32    0.96875
4  49  32    1.53125
5 -23  34  100.00000
6 -10 -29 -100.00000
7  10 -19  100.00000
8 -45 -48 -100.00000
9  31 -31  100.00000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM