我有以下数据帧
col1 col2 col3
a b c
d e f
g h i
我写了以下内容
df['col1'] = np.where((df['col2'].str.contains('b',case = False,regex=True,na=False)) &
(df['col3'].str.contains('c',case = False,regex=True,na=False)),'s', 'o')
我现在明白了
col1 col2 col3
s b c
o e f
o h i
我现在想做下面的事情,其中col1不等于s
df['col1'] = np.where((df['col1'] != 's') &
(df['col2'].str.contains('e',case = False,regex=True,na=False)) &
(df['col3'].str.contains('f',case = False,regex=True,na=False)),'z','x')
我想要以下
col1 col2 col3
s b c
z e f
x h i
但我得到了这个
col1 col2 col3
x b c
z e f
x h i
我希望逻辑不改变col1中的s
可能还有其他有效的解决方案,可能您可以尝试使用以下方法,其中如果
col1
等于s
,则返回s
,否则使用np.where
和其他条件:结果:
更新:
有关更多仍带有
where
的条件:使用应用程序:
首先我们可以创建函数,然后应用于
dataframe
:现在,将函数应用于数据帧:
结果:
相关问题 更多 >
编程相关推荐