擅长:python、mysql、java
<p>您只需通过两个步骤完成—设置混合列—然后将pos/neg列设置为0</p>
<pre><code>>>> df['Mixed'] = 0
>>> df
Positive Neutral Negative Mixed
0 1 0 1 0
1 0 1 0 0
>>> rows = (df.Positive == 1) & (df.Negative == 1)
>>> df.loc[rows, 'Mixed'] = 1
>>> df.loc[rows, ['Positive', 'Negative']] = 0
>>> df
Positive Neutral Negative Mixed
0 0 0 0 1
1 0 1 0 0
</code></pre>
<p>你可以用
<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mask.html#pandas-dataframe-mask" rel="nofollow noreferrer">^{<cd1>}</a>如果你想一起做</p>
<pre><code>>>> df
Positive Neutral Negative Mixed
0 1 0 1 0
1 0 1 0 0
>>> rows = (df.Positive == 1) & (df.Negative == 1)
>>> df.mask(rows, [0, 0, 0, 1])
Positive Neutral Negative Mixed
0 0 0 0 1
1 0 1 0 0
</code></pre>