Mathew K Analytics

Lesson 29 · Real-World Data Analytics

Airline Tweet Analysis in Python: What Makes Customers Complain? | Python Data Analytics #29

Real genuine customer tweets directed at a real airline, hunting for exactly what drives negative sentiment. Let's get into it. This lesson is for learners…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics 100, Video 29: Real Airline Customer Sentiment Analysis#

  • Video twenty-nine of the hundred-video real-world data analytics series.
  • Real genuine customer tweets directed at a real airline, hunting for exactly what drives negative sentiment.
  • Let's get into it.

This lesson is for learners who want to turn customer social media posts into a clear service priority. The business question is direct: how do customers feel about one airline on social media, and what drives their negative posts? A customer experience team would use the answer to decide which problem to fix first.

The dataset, airline_tweets_raw.csv, holds 341 tweets addressed to Virgin America, with 15 columns. The most important are airline_sentiment (negative, neutral or positive, labelled by human reviewers), airline_sentiment_confidence (how sure the labellers were, from 0 to 1), negativereason (the complaint category for negative tweets), negativereason_confidence, retweet_count, text, user_timezone and tweet_location.

In this lesson you will:

  • measure the sentiment mix and rank complaint reasons
  • audit label confidence and missing data before trusting the results
  • compare sentiment across timezones and engagement, and save a complaint summary

By the end you will be able to make a data-backed recommendation on where to focus. You should be comfortable with value_counts, groupby and boolean filtering; see earlier lessons in the series if not.

Part 1: Real Tweets, Real Complaints#

Before analysing sentiment you need to know the size and shape of what you have. The code imports pandas and matplotlib, reads the tweets file and checks .shape. The output (341, 15) shows 341 tweets and 15 columns. That is a small sample, so later percentages will move noticeably with just a few tweets. Keep the sample size in mind whenever you compare small groups, such as individual complaint reasons.

import pandas as pd
import matplotlib.pyplot as plt
tweets = pd.read_csv('airline_tweets_raw.csv')
tweets.shape
(341, 15)

Part 2: Real Data Preview#

With 15 columns, you need to know which ones are useful before you start. The code previews the sentiment and text columns with .head(5), then lists every column name with .columns.tolist(). Only the last expression is displayed, so the output shows the column list. Useful fields include airline_sentiment, negativereason, the two confidence columns, retweet_count, text and user_timezone. Columns ending in _gold look promising but turn out to be empty, as the missing data audit will show.

tweets[['airline_sentiment', 'text']].head(5)
tweets.columns.tolist()
['tweet_id',
 'airline_sentiment',
 'airline_sentiment_confidence',
 'negativereason',
 'negativereason_confidence',
 'airline',
 'airline_sentiment_gold',
 'name',
 'negativereason_gold',
 'retweet_count',
 'text',
 'tweet_coord',
 'tweet_created',
 'tweet_location',
 'user_timezone']

Part 3: Real Overall Sentiment Breakdown#

The sentiment mix is the headline figure any customer team asks for first. value_counts() counts how often each label appears and sorts from most to least common. The output shows 132 negative tweets, 113 neutral and 96 positive. Negative is the largest group, but not by a huge margin. This matters because customers who tweet at an airline are often complaining, so a negative majority is common in this kind of data rather than a sign of crisis on its own.

sentiment_counts = tweets['airline_sentiment'].value_counts()
sentiment_counts
airline_sentiment
negative    132
neutral     113
positive     96
Name: count, dtype: int64

Part 4: Real Sentiment as Percentages#

Percentages are easier to compare and communicate than raw counts. The code divides each count by the total number of tweets with len(tweets), multiplies by 100 and rounds to one decimal. The output shows 38.7% negative, 33.1% neutral and 28.2% positive. These three figures add up to 100%, which confirms every tweet has a sentiment label. Note that this table keeps the name count from the original Series, even though it now holds percentages.

sentiment_pct = round(sentiment_counts / len(tweets) * 100, 1)
sentiment_pct
airline_sentiment
negative    38.7
neutral     33.1
positive    28.2
Name: count, dtype: float64

Part 5: Visualizing Real Sentiment Breakdown#

A colour-coded bar chart makes the sentiment balance instantly readable. The code defines a dictionary that maps each sentiment to a colour, then builds a matching list of colours with a list comprehension so each bar is coloured by its label rather than by position. The figure is saved to sentiment_breakdown.png and closed, so nothing displays here. When you open it, look for the red negative bar standing somewhat taller than the grey neutral and green positive bars.

plt.figure(figsize=(7, 5))
colors = {'negative': 'crimson', 'neutral': 'gray', 'positive': 'seagreen'}
sentiment_counts.plot(kind='bar', color=[colors[s] for s in sentiment_counts.index])
plt.ylabel('Real Tweet Count')
plt.title('Real Customer Sentiment Breakdown')
plt.xticks(rotation=0)
plt.tight_layout()
plt.savefig('sentiment_breakdown.png', dpi=120)
plt.close()

Part 6: Real Negative Reason Breakdown#

Knowing that customers are unhappy is not enough: you need to know why. value_counts() on negativereason counts each complaint category, ignoring the empty values for non-negative tweets. The output shows Customer Service Issue far ahead with 42 tweets, followed by Flight Booking Problems with 22. Cancelled Flight and Can't Tell tie at 16. The counts add up to 132, matching the number of negative tweets. Can't Tell is a reminder that some complaints are too vague to classify.

reason_counts = tweets['negativereason'].value_counts()
reason_counts
negativereason
Customer Service Issue         42
Flight Booking Problems        22
Cancelled Flight               16
Can't Tell                     16
Late Flight                    13
Bad Flight                     10
Lost Luggage                    5
Flight Attendant Complaints     4
Damaged Luggage                 3
longlines                       1
Name: count, dtype: int64

Part 7: Visualizing Real Complaint Reasons#

A horizontal bar chart suits category labels that are long, such as complaint reasons. The code plots the counts with kind='barh' and calls plt.gca().invert_yaxis() so the most common reason sits at the top. The image is saved to negative_reasons.png and closed, so it does not display here. When you open it, look for how far the Customer Service Issue bar extends past the rest and how small the luggage and long-lines categories are.

plt.figure(figsize=(9, 6))
reason_counts.plot(kind='barh', color='darkorange')
plt.xlabel('Real Tweet Count')
plt.title('Real Negative Tweet Reasons')
plt.gca().invert_yaxis()
plt.tight_layout()
plt.savefig('negative_reasons.png', dpi=120)
plt.close()

Part 8: Real Top Complaint Reason#

Turning the top complaint into a share makes the priority easy to state. .idxmax() returns the label of the largest count, and dividing the maximum by the sum of all reason counts gives its share of negative tweets. The output shows Customer Service Issue accounts for 31.8% of negative tweets, nearly a third. That share is of negative tweets only, not of all 341 tweets, a distinction worth stating clearly when you report it.

top_reason = reason_counts.idxmax()
top_reason_pct = round(reason_counts.max() / reason_counts.sum() * 100, 1)
top_reason, top_reason_pct
('Customer Service Issue', np.float64(31.8))

Part 9: Real Sentiment Confidence Distribution#

Human labels are not always certain, so you should check how confident the labellers were. .describe() summarises the airline_sentiment_confidence column. The output shows an average of 0.888 and a median of 1.000, so more than half of the tweets were labelled with full agreement. However, the 25th percentile is 0.681 and the minimum 0.348, which means a quarter of labels carry noticeably less certainty. These are the rows to treat with care.

tweets['airline_sentiment_confidence'].describe().round(3)
count    341.000
mean       0.888
std        0.166
min        0.348
25%        0.681
50%        1.000
75%        1.000
max        1.000
Name: airline_sentiment_confidence, dtype: float64

Part 10: Real Low-Confidence Tweets#

Looking at actual low-confidence examples helps you judge whether the labels are trustworthy. The code filters tweets with confidence below 0.6. It then calls len(low_confidence), but that value is not displayed because it is not the last expression. The output shows three examples with confidence around 0.35, two labelled positive and one neutral. The visible text, such as complaints about commercials, suggests some of these labels are questionable. Use print() on the length if you want to see the count too.

low_confidence = tweets[tweets['airline_sentiment_confidence'] < 0.6]
len(low_confidence)
low_confidence[['airline_sentiment', 'airline_sentiment_confidence', 'text']].head(3)
airline_sentiment airline_sentiment_confidence text
1 positive 0.3486 @VirginAmerica plus you've added commercials t...
114 positive 0.3482 @VirginAmerica come back to #PHL already. We n...
142 neutral 0.3550 @VirginAmerica Can you find us a flt out of LA...

Part 11: Real Negative Reason Confidence#

Complaint reasons also come with a confidence score, and it is worth checking separately. .describe() on negativereason_confidence shows 167 non-missing values, an average of 0.577 and a median of 0.664. The minimum is 0.000 and the 25th percentile is 0.350. So labellers were much less certain about why a tweet was negative than whether it was negative. That means the reason breakdown is less reliable than the sentiment breakdown.

tweets['negativereason_confidence'].describe().round(3)
count    167.000
mean       0.577
std        0.359
min        0.000
25%        0.350
50%        0.664
75%        1.000
max        1.000
Name: negativereason_confidence, dtype: float64

Part 12: Real Retweet Volume by Sentiment#

Retweets show which posts spread beyond the original author, which matters for brand reputation. The code groups by airline_sentiment and takes the mean of retweet_count. The output shows very small averages: 0.008 for negative, 0.044 for neutral and 0.042 for positive. Almost no tweets were retweeted at all, so these differences rest on just a handful of retweets. Negative tweets spread the least here, but the numbers are too small to draw a firm conclusion.

retweets_by_sentiment = tweets.groupby('airline_sentiment')['retweet_count'].mean().round(3)
retweets_by_sentiment
airline_sentiment
negative    0.008
neutral     0.044
positive    0.042
Name: retweet_count, dtype: float64

Part 13: Real Most-Retweeted Tweet#

Checking the extreme case puts the averages in context. .idxmax() finds the row label of the highest retweet count, and .loc retrieves that whole row; the code then shows three of its fields. The output shows the most-retweeted tweet was positive with just 2 retweets, starting with Always have it together. That confirms retweet activity is extremely low in this sample, so retweets are not a useful measure of spread for this dataset.

most_retweeted = tweets.loc[tweets['retweet_count'].idxmax()]
most_retweeted[['airline_sentiment', 'retweet_count', 'text']]
airline_sentiment                                             positive
retweet_count                                                        2
text                 Always have it together!!! You're welcome! RT ...
Name: 147, dtype: object

Part 14: Real Word Count by Sentiment#

Tweet length can reveal how strongly people feel: frustrated customers often explain their problem in detail. The code splits each tweet on spaces with str(t).split() and counts the pieces, then averages by sentiment. The output shows negative tweets average 19.10 words, while neutral and positive average 13.25 and 13.27. Negative tweets are roughly 44% longer, which supports the idea that complaints carry more detail and may be more useful to read in full.

tweets['word_count'] = tweets['text'].apply(lambda t: len(str(t).split()))
wordcount_by_sentiment = tweets.groupby('airline_sentiment')['word_count'].mean().round(2)
wordcount_by_sentiment
airline_sentiment
negative    19.10
neutral     13.25
positive    13.27
Name: word_count, dtype: float64

Part 15: Real Timezone Distribution#

Timezones give a rough idea of where customers are, which can point to regional service issues. value_counts() on user_timezone ranks the zones, and .head(10) keeps the top ten. The output shows Pacific Time with 96 tweets, Eastern Time with 79 and Central Time with 25. Some entries, such as Sydney (11) and Quito (9), probably reflect user settings rather than actual location. The timezone is set by the user, so treat it as an approximate guide.

timezone_counts = tweets['user_timezone'].value_counts()
timezone_counts.head(10)
user_timezone
Pacific Time (US & Canada)     96
Eastern Time (US & Canada)     79
Central Time (US & Canada)     25
Sydney                         11
Quito                           9
Mountain Time (US & Canada)     7
Atlantic Time (Canada)          7
Arizona                         7
Alaska                          3
Caracas                         3
Name: count, dtype: int64

Part 16: Real Sentiment by Top Timezone#

Splitting sentiment by timezone shows whether some regions are unhappier than others. The code keeps only the top three timezones with .isin(), groups by timezone and sentiment, counts rows with .size(), then .unstack(fill_value=0) turns the sentiment level into columns, filling any gaps with 0. The output shows Pacific with 42 negative and 32 positive, while Eastern has 35 negative against 22 positive. Eastern customers lean more negative, worth investigating if the airline has East Coast operational problems.

top_timezones = timezone_counts.head(3).index
timezone_sentiment = tweets[tweets['user_timezone'].isin(top_timezones)].groupby(['user_timezone', 'airline_sentiment']).size().unstack(fill_value=0)
timezone_sentiment
airline_sentiment negative neutral positive
user_timezone
Central Time (US & Canada) 7 11 7
Eastern Time (US & Canada) 35 22 22
Pacific Time (US & Canada) 42 22 32

Part 17: Real Tweet Location Field#

Location data is often incomplete in social media datasets, so check coverage before using it. .notna() returns True for rows where tweet_location has a value, and .sum() counts the Trues. Dividing by the total and multiplying by 100 gives a percentage. The output shows 75.7% of tweets have a location. Like timezone, this field is free text typed by the user, so even when present it may be vague, misspelled or a joke.

has_location = tweets['tweet_location'].notna().sum()
has_location_pct = round(has_location / len(tweets) * 100, 1)
has_location_pct
np.float64(75.7)

Part 18: Real Missing Data Audit#

A missing data audit shows which columns you can rely on. tweets.isna().sum() counts missing values in every column, and sorting puts the worst first. The output shows the two _gold columns are empty in all 341 rows and tweet_coord is missing in 308. negativereason is missing in 209 rows, which is expected because only 132 tweets are negative. The new word_count column appears too, since it was added earlier. Text, sentiment and confidence are complete.

missing_summary = tweets.isna().sum().sort_values(ascending=False)
missing_summary
airline_sentiment_gold          341
negativereason_gold             341
tweet_coord                     308
negativereason                  209
negativereason_confidence       174
tweet_location                   83
user_timezone                    82
tweet_id                          0
name                              0
airline                           0
airline_sentiment                 0
airline_sentiment_confidence      0
text                              0
retweet_count                     0
tweet_created                     0
word_count                        0
dtype: int64

Part 19: Real Negative Reason Given Confidence Threshold#

Filtering to confident labels shows whether the complaint ranking still holds when you only trust the clearer cases. The code keeps rows where negativereason_confidence is at least 0.8 and counts reasons. The output still places Customer Service Issue first with 16 tweets, well ahead of Can't Tell and Cancelled Flight with 6 each. The ranking of the top reason is stable, which gives you more confidence in the main recommendation. Lower categories reshuffle, so do not over-interpret them.

high_confidence_reasons = tweets[tweets['negativereason_confidence'] >= 0.8]
high_confidence_reasons['negativereason'].value_counts()
negativereason
Customer Service Issue     16
Can't Tell                  6
Cancelled Flight            6
Flight Booking Problems     4
Late Flight                 4
Lost Luggage                4
Bad Flight                  3
Damaged Luggage             2
Name: count, dtype: int64

Part 20: Real Customer Service Complaints Deep Dive#

Counts tell you the size of a problem, but reading examples tells you what it actually is. The code filters for Customer Service Issue and shows the first five texts. The output includes complaints about seating, a status match request, lost headphones, waiting for a return phone call and chat support not working. That variety suggests the category covers response and contact problems more than one specific failure. Reading a sample of raw text is a habit worth building in every sentiment project.

cs_complaints = tweets[tweets['negativereason'] == 'Customer Service Issue']
cs_complaints[['text']].head(5)
text
24 @VirginAmerica you guys messed up my seating.....
25 @VirginAmerica status match program. I applie...
32 @VirginAmerica help, left expensive headphones...
33 @VirginAmerica awaiting my return phone call, ...
39 @VirginAmerica Your chat support is not workin...

Part 21: Real Average Confidence by Complaint Type#

Some complaint types may be easier to classify than others, which affects how much you should trust each count. The code groups by negativereason, takes the mean confidence and sorts from highest to lowest. The output shows Damaged Luggage (0.883) and Lost Luggage (0.867) are the clearest, while Flight Attendant Complaints are least certain at 0.507. Customer Service Issue sits at 0.759, fairly high, which supports treating it as a genuine top complaint.

confidence_by_reason = tweets.groupby('negativereason')['negativereason_confidence'].mean().round(3).sort_values(ascending=False)
confidence_by_reason
negativereason
Damaged Luggage                0.883
Lost Luggage                   0.867
Can't Tell                     0.766
Customer Service Issue         0.759
Cancelled Flight               0.756
Bad Flight                     0.717
longlines                      0.682
Flight Booking Problems        0.673
Late Flight                    0.652
Flight Attendant Complaints    0.507
Name: negativereason_confidence, dtype: float64

Part 22: Real Positive Tweet Sample#

Positive feedback shows what the airline is doing well and should protect. The code filters positive tweets and uses .sample(5, random_state=42) to pick five at random; random_state fixes the random choice so you get the same rows every time you run it. The output includes praise for a salt and pepper shaker, a gate-checked bag, 90s music on board and staff help. Positive comments often focus on small touches and staff, a useful contrast to the complaints.

positive_tweets = tweets[tweets['airline_sentiment'] == 'positive']
positive_tweets[['text']].sample(5, random_state=42)
text
273 @VirginAmerica cutest salt and pepper shaker e...
264 @VirginAmerica thanks for gate checking my bag...
248 @VirginAmerica love the 90s music blasting at ...
332 @VirginAmerica thanks for taking care of @Suup...
117 @VirginAmerica and again! Another rep kicked b...

Part 23: Real Mention Pattern Check#

Before comparing direct mentions with other tweets, you need to know how many mention the airline. .str.contains('@VirginAmerica', case=False, na=False) returns True for tweets containing the handle in any capitalisation, with na=False treating missing text as False. The mean of a boolean column is the share of Trues. The output shows 100.0%, so every tweet mentions the airline. That makes a direct-mention comparison impossible with this data.

tweets['mentions_airline'] = tweets['text'].str.contains('@VirginAmerica', case=False, na=False)
mention_rate = round(tweets['mentions_airline'].mean() * 100, 1)
mention_rate
np.float64(100.0)

Part 24: Real Sentiment Among Direct Mentions#

This step filters to tweets that mention the airline and uses value_counts(normalize=True) to return proportions instead of counts, multiplied by 100. Because every tweet mentions the airline, the output is identical to the overall breakdown: 38.7% negative, 33.1% neutral and 28.2% positive. That is a useful lesson in itself. Always check whether a filter actually removes any rows before you build a comparison on it.

mention_sentiment = tweets[tweets['mentions_airline']]['airline_sentiment'].value_counts(normalize=True).round(3) * 100
mention_sentiment
airline_sentiment
negative    38.7
neutral     33.1
positive    28.2
Name: proportion, dtype: float64

Part 25: Real Word Count vs Confidence Correlation#

You might suspect that longer tweets are easier for labellers to judge, because they contain more context. .corr() measures the Pearson correlation between word count and sentiment confidence, a value from -1 to 1. The output is 0.053, essentially zero. Tweet length has almost no relationship with how confident the labellers were. Low-confidence labels seem to come from ambiguous wording rather than from short tweets.

wordcount_confidence_corr = tweets['word_count'].corr(tweets['airline_sentiment_confidence'])
round(wordcount_confidence_corr, 3)
np.float64(0.053)

Part 26: Saving the Real Complaint Summary Table#

A summary table is the practical deliverable a customer team can work from. The code combines the reason counts and average confidence into one DataFrame; pandas lines them up by the reason names in the index, so the different sort orders do not matter. It saves the table to complaint_reason_summary.csv, reloads it with index_col=0 so the reasons become the index again, and compares shapes. The output True confirms the file round-trips correctly.

complaint_summary = pd.DataFrame({'count': reason_counts, 'avg_confidence': confidence_by_reason}).round(3)
complaint_summary.to_csv('complaint_reason_summary.csv')
reloaded_summary = pd.read_csv('complaint_reason_summary.csv', index_col=0)
reloaded_summary.shape == complaint_summary.shape
True

Part 27: Real Sanity Check, Sentiment Counts Sum Correctly#

Sanity checks confirm that no tweets were lost or double-counted. The code compares the total of the sentiment counts with the number of rows. The output np.True_ is NumPy's version of True, which appears because the sum is a NumPy number; it means the same as plain True. All 341 tweets have exactly one sentiment label, so the percentages reported earlier are based on the full dataset.

sentiment_counts.sum() == len(tweets)
np.True_

Part 28: Real Sanity Check, Confidence Scores in Range#

Confidence scores should always sit between 0 and 1, and values outside that range would point to a data error. The code builds two boolean comparisons, joins them with & (each side needs brackets) and wraps the result in all(), which returns True only if every row passes. The output True confirms every confidence score is valid. Remember that using and instead of & between two pandas Series raises an error.

all((0 <= tweets['airline_sentiment_confidence']) & (tweets['airline_sentiment_confidence'] <= 1))
True

Part 29: Real Priority Recommendation#

Analysis should end with a clear recommendation. The code takes the negative share from the percentage Series by its label, sentiment_pct['negative'], and builds a sentence with an f-string. The output states that with 38.7% of tweets negative and Customer Service Issue making up 31.8% of negatives, customer service is the area most worth investigating first. Supported by its stable ranking at high confidence, this is a recommendation you can defend.

negative_share = sentiment_pct['negative']
print(f'With {negative_share}% of tweets negative and {top_reason} the leading real complaint reason at {top_reason_pct}% of negatives, that is the real single area most worth investigating first.')
With 38.7% of tweets negative and Customer Service Issue the leading real complaint reason at 31.8% of negatives, that is the real single area most worth investigating first.

Part 30: Real Recap Print#

A recap sentence summarises the whole analysis in one line for a report or slide. The f-string pulls the stored values into the text so the sentence always matches the data. The output reads: across 341 customer tweets, 38.7% were negative, with Customer Service Issue driving 31.8% of those complaints. If you rerun the notebook on a newer export, this sentence updates automatically without any retyping.

print(f'Across {len(tweets)} real customer tweets, {negative_share}% were negative, with {top_reason} driving {top_reason_pct}% of those real complaints.')
Across 341 real customer tweets, 38.7% were negative, with Customer Service Issue driving 31.8% of those real complaints.

Wrap-Up: What You Learned#

  • Hand-labeled sentiment and complaint-reason fields, when available, are far richer than running sentiment analysis from scratch on raw real text.
  • Confidence scores attached to real labels are worth auditing separately, low-confidence rows deserve real skepticism before trusting them.
  • Retweet volume revealed which real sentiment category actually spreads furthest, a genuinely different question from which is most common.
  • Cross-tabulating sentiment against fields like timezone or direct-mention status can surface real patterns a single value count would miss.
  • A single real brand, deeply analyzed, can be just as instructive as a multi-brand comparison when the real data honestly supports that scope.
  • Next video: capstone, a real social media campaign performance report pulling together the real domain from this entire section.

Practice on your own

  • Rerun the sentiment breakdown using only tweets with airline_sentiment_confidence of 1.0 and compare the percentages with the full dataset.
  • Build a cross-tab of negativereason against user_timezone for the top three timezones and check whether Customer Service Issue leads in each one.
  • Count how many tweets in each sentiment group contain a question mark using .str.contains('?', regex=False) and see whether neutral tweets are mostly questions.

The next video in the series is a capstone: a social media campaign performance report that pulls together everything from this section.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.