擅长:python、mysql、java
<p>您可以创建一个字典,将旧值与新值配对,并通过pandas <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer">map</a>替换感伤列的内容</p>
<pre><code> #list of old and new values
old_values = [['Positive','positive', 'ppos'],
['Negative', 'negative', 'Neg'],
['Neutral', 'neutral', 'ne']]
new_values = ['positive','negative','neutral']
merge = zip(new_values,old_values)
#create mapping
d = {}
for new, old in merge:
for i in old:
d[i] = new
print(d)
{'Positive': 'positive',
'positive': 'positive',
'ppos': 'positive',
'Negative': 'negative',
'negative': 'negative',
'Neg': 'negative',
'Neutral': 'neutral',
'neutral': 'neutral',
'ne': 'neutral'}
#apply mapping to series:
df.Sentiment.map(d)
</code></pre>