Lesson 55 · Python for Data Science
5 - Survey Analysis in Python: Statistical Techniques for Data Science
Welcome! Today we will learn how to use Python for basic survey analysis. Analyzing survey data helps find trends, discover patterns, and answer real…
- CoursePython for Data Science
- Lesson55 of 38
- Video13 min
- FormatJupyter notebook · 23 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Survey Analysis in Python#
Welcome! Today we will learn how to use Python for basic survey analysis.
Analyzing survey data helps find trends, discover patterns, and answer real questions.
We will start with simple Python basics, and step-by-step build towards analyzing a sample survey.
No experience neededjust curiosity! Let us begin.
What is Survey Data?#
Survey data is information collected by asking people questions.
We can collect answers about favorite foods, study habits, hobbies, and more.
Analyzing this data can show us what most people like or need.
# Let us make a sample survey data set using a list of dictionaries.
survey_data = [
{'name': 'Alice', 'age': 25, 'favorite_color': 'blue', 'likes_pizza': True},
{'name': 'Bob', 'age': 30, 'favorite_color': 'green', 'likes_pizza': False},
{'name': 'Cathy', 'age': 22, 'favorite_color': 'red', 'likes_pizza': True},
{'name': 'Dan', 'age': 28, 'favorite_color': 'blue', 'likes_pizza': True},
{'name': 'Eva', 'age': 24, 'favorite_color': 'purple', 'likes_pizza': False},
]
print('Here is our sample survey data:')
for entry in survey_data:
print(entry)
# How many responses are there?
count = len(survey_data)
print('Number of survey responses:', count)
Why Use Lists and Dictionaries?#
Lists let us store many answers in one place.
Dictionaries let us label each answer neatly.
This combo makes it easy to work with real survey data.
# Let us find the average age.
ages = []
for entry in survey_data:
ages.append(entry['age'])
average_age = sum(ages) / len(ages)
print('Average age is:', average_age)
# Which color is most popular?
color_count = {}
for entry in survey_data:
color = entry['favorite_color']
if color not in color_count:
color_count[color] = 1
else:
color_count[color] += 1
print('Color counts:', color_count)
# Let us find out who likes pizza.
pizza_lovers = []
for entry in survey_data:
if entry['likes_pizza']:
pizza_lovers.append(entry['name'])
print('People who like pizza:', pizza_lovers)
# Let us filter survey data by age using input()
age_limit = int(input('Enter age limit to find participants older than: '))
older_participants = []
for entry in survey_data:
if entry['age'] > age_limit:
older_participants.append(entry['name'])
print('Participants older than', age_limit, ':', older_participants)
# What if someone did not answer one question?
survey_data.append({'name': 'Frank', 'age': 27, 'favorite_color': None, 'likes_pizza': True})
favorite_colors = []
for entry in survey_data:
if entry['favorite_color'] is not None:
favorite_colors.append(entry['favorite_color'])
print('Favorite colors with missing values skipped:', favorite_colors)
# Let us update Eva's favorite color to 'pink'.
for entry in survey_data:
if entry['name'] == 'Eva':
entry['favorite_color'] = 'pink'
print("Eva's updated survey info:", [e for e in survey_data if e['name'] == 'Eva'])
# Removing Frank's answer since it was blank
survey_data = [entry for entry in survey_data if entry['name'] != 'Frank']
print('Updated survey_data after removing Frank:')
for entry in survey_data:
print(entry)
Simple Built-in Functions for Analysis#
max()finds the highest value.min()finds the lowest value.set()removes duplicates.
Let us apply these to our survey!
# Let us find oldest and youngest participant.
ages = [entry['age'] for entry in survey_data]
print('Oldest:', max(ages))
print('Youngest:', min(ages))
# Gather all unique favorite colors.
unique_colors = set(entry['favorite_color'] for entry in survey_data)
print('Unique favorite colors:', unique_colors)
# Looping with enumerate for more info
for index, entry in enumerate(survey_data):
print('Entry number', index + 1, 'belongs to', entry['name'])
# List comprehensions: shorter way to get names who like pizza
pizza_lovers = [entry['name'] for entry in survey_data if entry['likes_pizza']]
print('Pizza lovers using list comprehension:', pizza_lovers)
# Sorting: order by age, youngest first
sorted_by_age = sorted(survey_data, key=lambda x: x['age'])
print('Survey sorted by age:')
for entry in sorted_by_age:
print(entry['name'], entry['age'])
# Filtering: only people who do not like pizza
not_pizza = [entry['name'] for entry in survey_data if not entry['likes_pizza']]
print('People who do not like pizza:', not_pizza)
# Mini-project: Simple summary of the survey
summary = {
'total_people': len(survey_data),
'average_age': round(sum(entry['age'] for entry in survey_data) / len(survey_data), 1),
'most_popular_color': max(
set(entry['favorite_color'] for entry in survey_data),
key=lambda c: [entry['favorite_color'] for entry in survey_data].count(c)
),
'pizza_lovers': len([entry for entry in survey_data if entry['likes_pizza']])
}
print('Survey Summary:')
for k, v in summary.items():
print(k, ':', v)
# Mini-project, part 2: Ask user for a color and count who chose it
target_color = input('Type a color to check how popular it was: ')
color_count = 0
for entry in survey_data:
if entry['favorite_color'] == target_color:
color_count += 1
print(target_color, 'was chosen by', color_count, 'participant(s).')
# Best practice: handle keys safely to avoid errors
for entry in survey_data:
color = entry.get('favorite_color', 'No answer')
print(entry['name'], 'picked', color)
# A common mistake: misspelled keys cause errors
try:
for entry in survey_data:
print(entry['fav_color']) # This key does not exist
except KeyError:
print('Oops! Key does not exist. Check your spelling.')
# Extra tip: make a quick stats table for all colors
color_stats = {}
for entry in survey_data:
color = entry['favorite_color']
color_stats[color] = color_stats.get(color, 0) + 1
for color, count in color_stats.items():
print('Color:', color, '| Count:', count)
Challenge Exercise#
Try editing the code to analyze another question.
For example: Who is the youngest pizza lover?
What is the most common first letter in participant names?
Play and practicethe best way to learn is by doing!
Recap#
Today you learned how to:
- Store and view survey results
- Count and filter answers
- Handle missing data
- Use built-in Python functions for analysis
- Summarize and present survey findings
Survey analysis helps us understand the people around us and make better choices!
Thank You for Learning Survey Analysis!#
If you enjoyed this video, please like, comment your favorite new skill, and subscribe for more Python lessons.
Share it with friends who want to learn Python or do data analysishelp others join in the fun.
Happy coding and see you next time!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



