根据与其他2列相关的条件创建列 [英] Create a column based on condition pertaining to 2 other columns
问题描述
我需要从这两个('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屋!