Sentiment Analysis: YouTube comments on NASA Mars landing

Anmol Hans
2 min readMar 7, 2021

Few days back I was astonished to learn that NASA was landing on the MARS. Because I cannot land on mars I did what I can do; the data analysis.

Download the csv file from Kaggle and start your Jupyter notebook.

Let’s Move straight to coding:

import pandas as pd
df=pd.read_csv('landing.csv')
print(df)

Data frame(df) is a table or a two dimensional array like structure in which data from CSV file is stored.

Output:

Output:Unnamed: 0      time                            comment
0 0 0:03 was uppp everyone
1 1 0:05 this is it
2 2 0:07 bobchamp
3 3 0:07 Omg Im so excited for this
4 4 0:07 french here
... ... ... ...
16125 16125 2:11:41 R I P David Bowie
16126 16126 2:11:41 _perfect_effect_ nice to meet you
16127 16127 2:11:41 its finished
16128 16128 2:11:42 Bye chat
16129 16129 2:11:43 Congratulations team Go NASA

[16130 rows x 3 columns]
import nltk
nltk.download("vader_lexicon")
from nltk.sentiment.vader import SentimentIntensityAnalyzer
vader = SentimentIntensityAnalyzer()

importing NLTK library and using vader function for sentiment analysis.

df["scores"] = df["comment"].apply(lambda comment: vader.polarity_scores(comment))
df.head()
df["compound"]=df["scores"].apply(lambda score_dict:score_dict["compound"])df["sentiment"]=df["compound"].apply(lambda c:"pos" if c>=0 else "neg")
print(df)

created a new column called “scores” to assign sentiment score to the YouTube comment.

Output:

Unnamed: 0      time                            comment  \
0 0 0:03 was uppp everyone
1 1 0:05 this is it
2 2 0:07 bobchamp
3 3 0:07 Omg Im so excited for this
4 4 0:07 french here
... ... ... ...
16125 16125 2:11:41 R I P David Bowie
16126 16126 2:11:41 _perfect_effect_ nice to meet you
16127 16127 2:11:41 its finished
16128 16128 2:11:42 Bye chat
16129 16129 2:11:43 Congratulations team Go NASA

scores compound sentiment
0 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
1 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
2 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
3 {'neg': 0.0, 'neu': 0.616, 'pos': 0.384, 'comp... 0.4795 pos
4 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
... ... ... ...
16125 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
16126 {'neg': 0.0, 'neu': 0.588, 'pos': 0.412, 'comp... 0.4215 pos
16127 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
16128 {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound... 0.0000 pos
16129 {'neg': 0.0, 'neu': 0.435, 'pos': 0.565, 'comp... 0.5994 pos

3 new columns were added, “scores”, “compound”, “sentiment” for the analysis of the comments were made immediately after NASA perseverance landed on Mars.

--

--