在pandas数据框中将列值拆分为几个

时间:2019-02-04 16:05:09

标签: pandas dataframe lambda sentiment-analysis

我正在尝试使用nltk.sentiment.vader中的sentimentIntensityAnalyzer()对推文进行情感分析

sid = SentimentIntensityAnalyzer()


listy = []

for index, row in data.iterrows():
  ss = sid.polarity_scores(row["Tweets"])
  listy.append(ss)

se = pd.Series(listy)
data['polarity'] = se.values

display(data.head(100))

这是生成的dataFramee:

    Tweets  polarity
0   RT @spectatorindex: Facebook controls:\n\n- Wh...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
1   RT @YAATeamWest: Today we're at @BradfordUniSU...   {'neg': 0.0, 'neu': 0.902, 'pos': 0.098, 'comp...
2   #SachinTendulkar launches India’s first Multip...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
3   How To Create a 360 R​ender (And How to Improv...   {'neg': 0.0, 'neu': 0.722, 'pos': 0.278, 'comp...
4   The Most Disturbing Virtual Reality You Will E...   {'neg': 0.174, 'neu': 0.826, 'pos': 0.0, 'comp...
5   VR Training for Troops \n\n...    {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
6   RT @DefenceHQ: The @BritishArmy has awarded a ...   {'neg': 0.0, 'neu': 0.847, 'pos': 0.153, 'comp...
7   RT @UofGHumanities: @UofGCSPE Humanities Lectu...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
8   RT @OyezServices: Ever wanted a tour of Machu ...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
9   RT @ProjectDastaan: We are an Oxford Universit...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...
10  RT @Paula_Piccard: Virtual reality will change...   {'neg': 0.0, 'neu': 0.878, 'pos': 0.122, 'comp...

为了对极性列中的“ neg”,“ pos”,“ neu”和“ compound”实体进行统计分析,我想将数据分为四个不同的列。为此,我使用了:

list_pos= []
list_neg = []
list_comp = []
list_neu = []
for index, row in data.iterrows():
  list_pos.append(row['polarity']['pos'])
  list_neg.append(row['polarity']['neg'])
  list_comp.append(row['polarity']['compound'])
  list_neu.append(row['polarity']['neu'])
se_pos = pd.Series(list_pos)
se_neg = pd.Series(list_neg)
se_comp = pd.Series(list_comp)
se_neu = pd.Series(list_neu)
data['positive'] = se_pos.values
data['negative'] = se_neg.values
data['compound'] = se_comp.values
data['neutral'] = se_neu.values

结果dataFrame:


Tweets  polarity    positive    negative    compound    neutral
0   RT @spectatorindex: Facebook controls:\n\n- Wh...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...   0.000   0.000   0.0000  1.000
1   RT @YAATeamWest: Today we're at @BradfordUniSU...   {'neg': 0.0, 'neu': 0.902, 'pos': 0.098, 'comp...   0.098   0.000   0.3612  0.902
2   #SachinTendulkar launches India’s first Multip...   {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound...   0.000   0.000   0.0000  1.000

是否有更简洁的方法来实现类似的dataFrame?也许使用lambda函数?谢谢您的帮助!

0 个答案:

没有答案