擅长:python、mysql、java
<p>下面是一种将<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html" rel="nofollow noreferrer">^{<cd1>}</a>和<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html" rel="nofollow noreferrer">^{<cd2>}</a>与<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.where.html" rel="nofollow noreferrer">^{<cd3>}</a>一起使用的方法:</p>
<pre><code>cols = ['col1', 'col2', 'col3', 'col4']
df['b'] = (df[cols].where(df[cols].stack().str.contains('b')
.unstack(fill_value=False)).ffill(1).iloc[:,-1])
</code></pre>
<hr/>
<pre><code>print(df)
ID col0 col1 col2 col3 col4 col5 b
0 1 jack a/h t/m w/n y/h 56 NaN
1 2 sam z/n b/w NaN NaN 93 b/w
2 3 john b/i y/d p/d NaN 33 b/i
</code></pre>