if语句python用于列值

时间:2018-11-29 06:26:42

标签: python pandas function loops if-statement

我正在尝试在python中编写以下逻辑:

如果列A为“ 123”,列B为“ 456”,则列c = 0

我尝试了以下功能,但返回错误:

 def testfunc(df):
     if df['columna'] == 123:
         if df['columnb'] ==456:
             df['columnc']=0

 return df
 testfunc()

错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我在做什么错?

1 个答案:

答案 0 :(得分:1)

numpy.where和链条条件与&一起用于按位AND

如果存在列columnc,则解决方案:

def testfunc(df):
    m1 = df['columna'] == 123
    m2 = df['columnb'] == 456
    df['columnc']= np.where(m1 & m2, 0, df['columnc'])
    return df

如果不存在,则必须定义两个值,例如010

def testfunc(df):
    m1 = df['columna'] == 123
    m2 = df['columnb'] == 456
    df['columnc']= np.where(m1 & m2, 0, 10)
    return df

示例

df = pd.DataFrame({
         'columna':[123,123,4,5],
         'columnb':[456,8,456,4],
         'columnc':[1,3,5,7],

})
print (df)
   columna  columnb  columnc
0      123      456        1
1      123        8        3
2        4      456        5
3        5        4        7

def testfunc(df):
    m1 = df['columna'] == 123
    m2 = df['columnb'] == 456
    df['columnc']= np.where(m1 & m2, 0, df['columnc'])
    return df

df1 = testfunc(df)
print (df1)
   columna  columnb  columnc
0      123      456        0
1      123        8        3
2        4      456        5
3        5        4        7
def testfunc(df):
    m1 = df['columna'] == 123
    m2 = df['columnb'] == 456
    df['columnc']= np.where(m1 & m2, 0, 10)
    return df

df1 = testfunc(df)
print (df1)
   columna  columnb  columnc
0      123      456        0
1      123        8       10
2        4      456       10
3        5        4       10