CSS 1022 DATA VISUALIZATION
BTech Computer Science Stream , Feburary 2026
Week 9 - Data Visualization: String Manipulation
Instructor: Dr. V. Sivakumar , Date: 23/03/2026
Importance of String Processing in Sentiment Analysis
Most real-world datasets contain textual (string) data in addition to numerical
values.
In sentiment analysis, text data (reviews/comments) carries meaningful information
about user opinions.
String values can represent categories such as positive, negative, and neutral
sentiments.
Pandas provides efficient tools to handle and process string data, including support
for categorical data types.
String preprocessing techniques (lowercasing, removing punctuation, stopword
removal) help in cleaning the data.
These steps enable better analysis, helping to extract insights such as frequent
words and sentiment patterns.
In [12]: # Import necessary libraries
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
import string # predefined string constants and helper functions.
from collections import Counter # Counter is a specialized dictionary from the c
In [13]: words = ["apple", "banana", "apple"]
count = Counter(words) #Counter is a class imported from the collections module.
count
Out[13]: Counter({'apple': 2, 'banana': 1})
In [14]: # Sample dataset (Replace with your dataset file if available)
#A dictionary named data is created.
data = {
'text': [
'I love this product! It is amazing.',
'Worst experience ever. Totally disappointed.',
'It is okay, not great but not bad.',
'Absolutely fantastic service!',
'Terrible, will not buy again.'
],
'sentiment': ['positive', 'negative', 'neutral', 'positive', 'negative']
}
df = [Link](data) #converts the dictionary into a Pandas DataFrame.
print("Dataset Preview:")
print([Link]())
Dataset Preview:
text sentiment
0 I love this product! It is amazing. positive
1 Worst experience ever. Totally disappointed. negative
2 It is okay, not great but not bad. neutral
3 Absolutely fantastic service! positive
4 Terrible, will not buy again. negative
In [15]: df
Out[15]: text sentiment
0 I love this product! It is amazing. positive
1 Worst experience ever. Totally disappointed. negative
2 It is okay, not great but not bad. neutral
3 Absolutely fantastic service! positive
4 Terrible, will not buy again. negative
In [16]: # Step 2: Basic string operations
# Convert to lowercase
df['clean_text'] = df['text'].[Link]()
# Remove punctuation
df['clean_text'] = df['clean_text'].apply(lambda x: [Link]([Link](''
# applies a function to each row of the clean_text [Link] all characters
print("\nCleaned Text:")
print(df[['text', 'clean_text']])
Cleaned Text:
text \
0 I love this product! It is amazing.
1 Worst experience ever. Totally disappointed.
2 It is okay, not great but not bad.
3 Absolutely fantastic service!
4 Terrible, will not buy again.
clean_text
0 i love this product it is amazing
1 worst experience ever totally disappointed
2 it is okay not great but not bad
3 absolutely fantastic service
4 terrible will not buy again
In [17]: # Step 3: Count sentiments
sentiment_counts = df['sentiment'].value_counts()
print("\nSentiment Counts:")
print(sentiment_counts)
Sentiment Counts:
sentiment
positive 2
negative 2
neutral 1
Name: count, dtype: int64
In [18]: # Step 4: Frequent words in positive reviews
positive_text = ' '.join(df[df['sentiment'] == 'positive']['clean_text']) #Combi
words = positive_text.split() # breaks the string into a list of individual word
word_freq = Counter(words) #Identifies commonly used words in positive reviews.
print("\nTop words in positive reviews:")
print(word_freq.most_common(5))
Top words in positive reviews:
[('i', 1), ('love', 1), ('this', 1), ('product', 1), ('it', 1)]
In [19]: # Step 5: Remove stopwords
stopwords = {'is', 'it', 'this', 'the', 'a', 'an', 'not', 'but'}
def remove_stopwords(text):
return ' '.join([word for word in [Link]() if word not in stopwords]) #K
df['no_stopwords'] = df['clean_text'].apply(remove_stopwords)
print("\nText after removing stopwords:")
print(df[['clean_text', 'no_stopwords']])
Text after removing stopwords:
clean_text \
0 i love this product it is amazing
1 worst experience ever totally disappointed
2 it is okay not great but not bad
3 absolutely fantastic service
4 terrible will not buy again
no_stopwords
0 i love product amazing
1 worst experience ever totally disappointed
2 okay great bad
3 absolutely fantastic service
4 terrible will buy again
In [20]: # Step 6: Word length distribution
all_words = ' '.join(df['no_stopwords']).split() #Splits the long string into in
word_lengths = [len(word) for word in all_words]
[Link]()
[Link](word_lengths)
[Link]('Word Length Distribution')
[Link]('Word Length')
[Link]('Frequency')
[Link]()
#To understand how long the words are in your dataset
In [14]: # Step 7: Bar chart of sentiment count
[Link]()
sentiment_counts.plot(kind='bar')
[Link]('Sentiment Distribution')
[Link]('Sentiment')
[Link]('Count')
[Link]()
In [15]: # Step 8: Keywords influencing negative sentiment
negative_text = ' '.join(df[df['sentiment'] == 'negative']['no_stopwords'])
neg_words = negative_text.split()
neg_freq = Counter(neg_words)
print("\nTop keywords in negative sentiment:")
print(neg_freq.most_common(5))
Top keywords in negative sentiment:
[('worst', 1), ('experience', 1), ('ever', 1), ('totally', 1), ('disappointed',
1)]
In [16]: # Step 9: Summary
summary = """
String processing improves sentiment analysis by cleaning raw text data.
Lowercasing ensures uniformity, punctuation removal eliminates noise,
and stopword removal focuses on meaningful words.
These steps help identify key patterns and frequent words that influence sentime
leading to more accurate analysis.
"""
print("\nSummary:")
print(summary)
Summary:
String processing improves sentiment analysis by cleaning raw text data.
Lowercasing ensures uniformity, punctuation removal eliminates noise,
and stopword removal focuses on meaningful words.
These steps help identify key patterns and frequent words that influence sentimen
t,
leading to more accurate analysis.