<p><strong>TL;博士:</strong></p>
<pre><code>df['newColumn'] = np.where((df.compareColumn.isin(yourlist)), TrueValue, FalseValue)
</code></pre>
<hr/>
<p>另一个一步的方法是使用<a href="https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.where.html#numpy-where" rel="nofollow noreferrer">^{<cd1>}</a>和<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.isin.html#pandas-dataframe-isin" rel="nofollow noreferrer">^{<cd2>}</a>。</p>
<pre><code>import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 1, 2, 2, 3, 3],
'B': [10, 15, 20, 25, 30,35],
'C': [100, 150, 200, 250, 300, 350]})
df['D'] = np.where((df.B.isin(firsts)), 1, 0)
</code></pre>
<p>我们使用<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.isin.html#pandas-dataframe-isin" rel="nofollow noreferrer">^{<cd2>}</a>中的返回作为<a href="https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.where.html#numpy-where" rel="nofollow noreferrer">^{<cd1>}</a>中的条件来返回</p>
<ul>
<li><code>1</code>当<code>True</code>时</li>
<li><code>0</code>当<code>False</code>时</li>
</ul>
<p>并将它们分配给同一数据帧中的新列<code>df['D']</code>。</p>
<p><strong>注意:</strong><code>np.where</code>允许使用<a href="https://www.programiz.com/python-programming/operators" rel="nofollow noreferrer">bitwise</a>运算符和替换用例的更复杂条件,即<code>False</code>上的“旁路”</p>
<pre><code>df['col1'] = np.where(((df['col1'] == df['col2']) &
(~df['col1'].str.startswith('r'))),
'replace', df['col1'])
</code></pre>
<hr/>