Skip to content
background-image background-image

Analyzing Customer Survey Data from CSV Using Pandas

This example demonstrates the use of Python's Pandas library to extract and analyze customer survey data provided in CSV format.

Introduction

Backstory

Imagine you are a data analyst at a small online bookstore called "BookHaven." Your team is interested in analyzing the sales data of different book genres to make informed decisions about inventory management and marketing strategies.

Motivation

The team recently conducted a customer survey, and you've received a CSV-formatted string containing survey responses. Each row in the CSV represents a customer's response, including their name, age, favorite book genre, and whether they've made a purchase in the past month. Your task is to extract and analyze this data using Pandas.

Statement

On input data, we have a CSV-formatted string with the following structure:

Name,Age,Favorite_Genre,Has_Purchased
Alice,28,Mystery,Yes
Bob,22,Fantasy,Yes
Carol,35,Romance,No
David,45,Science Fiction,Yes
Emily,29,Science Fiction,Yes
Frank,19,Fantasy,No
Grace,40,Mystery,No

Input data has the following structure:

"data": string

import pandas as pd
from io import StringIO

# Convert the CSV string into a Pandas DataFrame
data = pd.read_csv(StringIO(INPUT_DATA[0]["data"]))

# Display the first few rows of the DataFrame
log.info(data.head())

# Analyze the data
total_customers = len(data)
purchased_customers = len(data[data["Has_Purchased"] == "Yes"])
average_age = data["Age"].mean()

# Analysis Results
log.info(f"Total Customers: {total_customers}")
log.info(f"Customers Who Made a Purchase: {purchased_customers}")
log.info(f"Average Age of Customers: {average_age:.2f} years")

Explanation

  • Converting CSV to DataFrame: We use Pandas to read the CSV string and convert it into a DataFrame for easy data manipulation.
  • Displaying Data: We display the first few rows of the DataFrame to understand the structure of the data.
  • Data Analysis: We calculate and log key statistics such as the total number of customers, the number of customers who made a purchase, and the average age of customers.

Conclusion

This Pandas-based analysis of customer survey data enables data analysts to extract insights quickly and efficiently. By leveraging Pandas' capabilities, you can explore and understand your data, which can inform decisions related to inventory management and marketing strategies for your online bookstore or similar businesses.