if-else用于多个条件数据帧

时间:2019-06-10 11:25:50

标签: python pandas

我不知道如何正确纠正以下想法: 我有一个具有两列和许多行的数据框。 我想基于这两列中的数据创建一个新列,以便如果其中之一为1,则该值为1,否则为0。 像这样:

if (df['col1']==1 | df['col2']==1):
   df['newCol']=1
else:
   df['newCol']=0

我尝试以不同的方式使用.loc函数,但是出现了不同的错误,因此我未正确使用它,或者这不是正确的解决方案...

感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

只需使用np.wherenp.select

df['newCol'] = np.where((df['col1']==1 | df['col2']==1), 1, 0)

OR

df['newCol'] = np.select([cond1, cond2, cond3], [choice1, choice2, choice3], default=def_value)

当特定条件为true时,请替换为对应的选择(np.select)。

答案 1 :(得分:0)

使用192.168.xxx.x - - [10/Jun/2019:12:40:15 +0100] "GET /company-publications/152005 HTTP/1.1" 200 55848 "google.com" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6" "xx.xx.xx.xx" 解决此问题的一种方法,

.loc

如果要使用newcol作为字符串,

df.loc[(df['col1'] == 1 | df['col2']==1) ,'newCol'] = 1
df['newCol'].fillna(0,inplace=True)

df.loc[(df['col1'] == 1 | df['col2']==1) ,'newCol'] = '1'
df['newCol'].fillna('0',inplace=True)