擅长:python、mysql、java
<p>另一个,熊猫解决方案没有循环:</p>
<pre><code>df = df.set_index('id')
rep = {'Positive':1,'Negative':-1,'Neutral':0}
df1 = df.replace(rep)
df = df.assign(Winner=np.where(df1.sum(axis=1) > 0,'Positive',np.where(df1.sum(axis=1) < 0, 'Negative', df.iloc[:,0])))
print(df)
</code></pre>
<p>输出:</p>
^{pr2}$
<h2>解释</h2>
<p><code>df.assign</code>是在原始数据帧的副本中创建列的一种方法,因此必须重新分配回df。列的名称是<code>Winner</code>,因此是“winner=”。在</p>
<p>接下来,使用<a href="https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.where.html" rel="nofollow noreferrer">^{<cd3>}</a>嵌套if语句。。。np.哪里(条件、结果、其他)</p>
<pre><code>np.where(df.sum(axis=1) > 0, # this sums the dataframe by row
'Positive', #if true
np.where(df.sum(axis=1) < 0, #nested if the first if return false
'Negative', #sum of the row is less than 0
df.iloc[:,0] #sum = 0 get the first value from that row.
)
)
</code></pre>