根据与其他2列相关的条件创建列 [英] Create a column based on condition pertaining to 2 other columns

查看:141
本文介绍了根据与其他2列相关的条件创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pandas DataFrame中有两列(我们调用'col1'和col2')。两者都包含True / False值。



我需要从这两个('col3')创建一个第三列,如果两列中的一个或另一列具有这个记录中的真值。



目前,我正在这样做:

  col3 = [] 

为索引,df.iterrows()中的行:
如果df.ix [index,'col1'] == True或df.ix [index,'col2'] == True:
col3.append(True)
else:
col3.append(False)

df ['col3' ] = col3

它对我的数据集的大小有效,但是有什么办法它以一线/矢量化的方式?可能使用两个嵌套的 np.where()语句?

解决方案

你可以使用 np.logical_or 执行此操作:

 在[236]中:
df = pd.DataFrame {'col1':[True,False,False],'col2':[False,True,False]})
df

输出[236]:
col1 col2
0 True False
1 False True
2 False False

在[239]中:
df ['col3'] = np.logical_or(df ['col1'],df ['col2'])
df

输出[239]:
col1 col2 col3
0 True False True
1 False True True
2 False False False

或使用 | 运算符:

 在[240]中:
df ['col3'] = df ['col1'] | df ['col2']

df
Out [240]:
col1 col2 col3
0 True False True
1 False True True
2假虚假


I have two columns in a pandas DataFrame (let's call the 'col1' and col2'). Both contain True/False values.

I need to create a third column from these two ('col3'), that will have a True value for a record if one or the other of the two columns has a True value in that record.

Currently, I'm doing this with:

col3 = []

for index, row in df.iterrows():
    if df.ix[index, 'col1'] == True or df.ix[index, 'col2'] == True:
        col3.append(True)
    else:
        col3.append(False)

df['col3'] = col3

It works fast enough for the size of my dataset, but is there any way to do it in a one-liner/vectorized way? Perhaps using two nested np.where() statements?

解决方案

You can use np.logical_or to do this:

In [236]:
df = pd.DataFrame({'col1':[True,False,False], 'col2':[False,True,False]})
df

Out[236]:
    col1   col2
0   True  False
1  False   True
2  False  False

In [239]:
df['col3'] = np.logical_or(df['col1'], df['col2'])
df

Out[239]:
    col1   col2   col3
0   True  False   True
1  False   True   True
2  False  False  False

or use | operator:

In [240]:
df['col3'] = df['col1'] | df['col2']

df
Out[240]:
    col1   col2   col3
0   True  False   True
1  False   True   True
2  False  False  False

这篇关于根据与其他2列相关的条件创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆