Python 在Pandas中,如何根据基于两个现有列的条件填充新列?
我正试图根据两个现有列的条件创建一个新列,但在使用“np.where”后出现了一个错误,是否还有其他方法可以实现这一点 输入:Python 在Pandas中,如何根据基于两个现有列的条件填充新列?,python,pandas,dataframe,Python,Pandas,Dataframe,我正试图根据两个现有列的条件创建一个新列,但在使用“np.where”后出现了一个错误,是否还有其他方法可以实现这一点 输入: change1 change2 yes yes yes no no yes no yes 预期产出: change1 change2 change3 yes yes ok yes no not ok no yes not ok no yes no
change1 change2
yes yes
yes no
no yes
no yes
预期产出:
change1 change2 change3
yes yes ok
yes no not ok
no yes not ok
no yes not ok
代码:
错误:
cannot compare a dtyped [object] array with a scalar of type [bool]
使用和。
这将帮助您改进代码的语法并避免错误
df['change3'] = np.where(df.eq('yes').all(axis=1), 'ok' , 'not ok')
#if you need select columns
#df['change3'] = np.where(df[['change1', 'change2']].eq('yes').all(axis=1),
'ok' , 'not ok')
没有
或
您也可以使用/
您可以使用:
df['change3'] = df.apply(lambda x: 'ok' if x['change1'] == x['change2'] else 'not ok', axis=1)
输出:
使用
数据帧。替换以转换为二进制,然后选中每行的all
:
df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).all(axis=1),
'ok',
'not ok')
或使用替换
和求和
:
df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).sum(axis=1).gt(1),
'ok',
'not ok')
你缺少括号,这被称为:np。其中((df['change1']='yes')&(df['change2']='yes'),'ok','notok')
为什么要投否决票?同意ansev,除非绝对必要,否则我们不应该发布申请案例。我早期的一些pandas代码被的答案大量篡改。在不需要的地方应用。也许我应该对矢量化有更多的了解,但这是我们应该引导初学者远离的东西。不管是谁否决了这一点,这都是不必要的,这个答案是正确的,尽管apply
并不可取。我投了更高的票。我喜欢你的例子,替换和求和+1
df['change3'] = df.eq('yes').all(axis=1).map({True : 'ok' , False : 'not ok'})
#df['change3'] = df.eq('yes').all(axis=1).replace({True : 'ok' , False : 'not ok'})
print(df)
# change1 change2 change3
# 0 yes yes ok
# 1 yes no not ok
# 2 no yes not ok
# 3 no yes not ok
df['change3'] = df.apply(lambda x: 'ok' if x['change1'] == x['change2'] else 'not ok', axis=1)
df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).all(axis=1),
'ok',
'not ok')
df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).sum(axis=1).gt(1),
'ok',
'not ok')
change1 change2 change3
0 yes yes ok
1 yes no not ok
2 no yes not ok
3 no yes not ok