擅长:python、mysql、java
<p>您可以使用<code>isin</code>方法:</p>
<pre><code>df['D'] = df.C.isin(firsts).astype(int)
df
# A B C D
#0 1 10 100 1
#1 1 15 150 0
#2 2 20 200 1
#3 2 25 250 0
#4 3 30 300 1
#5 3 35 350 0
</code></pre>
<hr/>
<p>您的方法失败的原因是python<code>in</code>运算符检查序列的索引而不是值,这与字典的工作方式相同:</p>
<pre><code>firsts
#A
#1 100
#2 200
#3 300
#Name: C, dtype: int64
1 in firsts
# True
100 in firsts
# False
2 in firsts
# True
200 in firsts
# False
</code></pre>
<p>修改方法如下:</p>
<pre><code>firstSet = set(firsts)
df['C'].apply(lambda x: 1 if x in firstSet else 0)
#0 1
#1 0
#2 1
#3 0
#4 1
#5 0
#Name: C, dtype: int64
</code></pre>