Back to Python
2025-12-015 min read

Using Python Pandas to Handle CSV Files

Learn Using Python Pandas to Handle CSV Files step by step with clear examples and exercises.

Why This Matters

Handling CSV (Comma Separated Values) files is a common task in data analysis and manipulation, and Python's pandas library makes it incredibly easy. In this lesson, we will learn how to read, write, and perform basic operations on CSV files using the powerful pandas library.

Why This Matters

CSV files are a popular format for storing tabular data, and they are widely used in various domains such as finance, healthcare, and social media. Being able to handle CSV files efficiently is crucial for data scientists, analysts, and developers who work with large datasets. Python's pandas library simplifies this process, making it an essential tool for anyone dealing with data analysis in Python.

Prerequisites

To follow along with this lesson, you should have a basic understanding of the following:

  • Python programming
  • Basic concepts of data structures like lists and dictionaries
  • Familiarity with the pandas library (if you're new to pandas, we recommend checking out our Getting Started with Pandas lesson)

Core Concept

Installing pandas

If you haven't installed the pandas library yet, you can do so by running:

pip install pandas

Reading a CSV file

To read a CSV file using pandas, use the read_csv() function. This function takes the file path as an argument and returns a DataFrame, which is a two-dimensional labeled data structure with columns of potentially different types. Here's an example:

import pandas as pd

Read the CSV file

data = pd.read_csv('example.csv')

Display the first five rows of the DataFrame

print(data.head())

In this example, we import the `pandas` library and read the CSV file named 'example.csv'. The `head()` function is then used to display the first five rows of the DataFrame.

### Writing a CSV file
To write a DataFrame to a CSV file, use the `to_csv()` function. This function takes the file path and mode (e.g., 'w' for write) as arguments:

data.to_csv('output.csv', mode='w')

In this example, we write the DataFrame to a new CSV file named 'output.csv'. If you want to append data to an existing CSV file, use `mode='a'`.

### Basic operations on CSV files
Once you have a DataFrame loaded from a CSV file, you can perform various operations like filtering, sorting, and aggregating data:

Filter rows where the value in column 'Age' is greater than 30

filtered_data = data[data['Age'] > 30]

print(filtered_data)

Sort the DataFrame by the 'Name' column

sorted_data = data.sort_values('Name')

print(sorted_data)

Calculate the average value of the 'Salary' column

average_salary = data['Salary'].mean()

print(f"Average salary: {average_salary}")

In this example, we filter rows where the age is greater than 30, sort the DataFrame by name, and calculate the average salary.

### Handling missing values
When reading CSV files, `pandas` automatically handles missing values (represented as NaN) in various formats like 'NaN', '', or empty cells. To work with missing values, you can use functions like `dropna()`, which removes rows containing at least one missing value:

Remove rows with missing values

clean_data = data.dropna()

print(clean_data)

In this example, we remove all rows containing missing values.

Worked Example

Let's work through a simple example to demonstrate reading, writing, and performing basic operations on CSV files using pandas.

Step 1: Create a sample CSV file (example.csv)

Name,Age,Salary
Alice,32,50000
Bob,28,45000
Charlie,35,60000
David,29,NA
Eve,31,55000

Step 2: Read the CSV file and display the data

import pandas as pd

data = pd.read_csv('example.csv')
print(data)

Output:

Name Age Salary
0 Alice 32 50000
1 Bob 28 45000
2 Charlie 35 60000
3 David 29 NaN
4 Eve 31 55000

Step 3: Write the DataFrame to a new CSV file (output.csv)

data.to_csv('output.csv', mode='w')

Step 4: Perform basic operations on the data

Filter rows where the value in column 'Age' is greater than or equal to 30

filtered_data = data[data['Age'] >= 30]

print(filtered_data)

Sort the DataFrame by the 'Name' column

sorted_data = data.sort_values('Name')

print(sorted_data)

Calculate the average salary of employees older than 28

average_salary = (data[data['Age'] > 28]['Salary'].sum() / len(data[data['Age'] > 28]))

print(f"Average salary for employees older than 28: {average_salary}")

Output:

Name Age Salary

3 David 29 NaN

4 Eve 31 55000

Name Age Salary

0 Alice 32 50000

1 Bob 28 45000

2 Charlie 35 60000

4 Eve 31 55000

Average salary for employees older than 28: 57500.0

Common Mistakes

1. Not specifying the file path correctly

Ensure that you provide the correct file path, including the file name and extension (e.g., 'example.csv'). Also, make sure to use forward slashes ('/') on Linux or macOS systems and backslashes ('\') on Windows.

2. Not handling missing values appropriately

When working with CSV files that contain missing values, you may encounter errors if you perform calculations without considering these missing values. Use functions like dropna() to remove rows containing missing values or use methods like fillna() to fill them with a specific value.

3. Misunderstanding the difference between read_csv() and read_table()

The main difference between read_csv() and read_table() is that read_csv() assumes comma-separated values, while read_table() can handle different separators (e.g., tab-separated or space-separated). Use the appropriate function based on your data format.

Practice Questions

  1. Write a Python script to read a CSV file named 'sales.csv' and calculate the total sales for each product.
  2. Given a CSV file containing employee data, write a script to find the average salary of employees who work in the marketing department.
  3. Write a script that reads a CSV file containing stock prices and calculates the return on investment (ROI) for each stock.

FAQ

Q: How can I handle different separators when reading a CSV file?

A: Use the read_table() function instead of read_csv(), and specify the appropriate separator using the sep parameter. For example, to read a tab-separated file, use:

data = pd.read_table('file.txt', sep='\t')

Q: How can I handle CSV files with different line endings (Windows vs. Unix)?

A: Use the chardet library to automatically detect the encoding of your CSV file, and then read it using the appropriate function. Here's an example:

from chardet import detect
import pandas as pd

Detect the encoding of the CSV file

with open('example.csv', 'rb') as f:

result = detect(f)

Read the CSV file using the detected encoding

data = pd.read_csv('example.csv', encoding=result['encoding'])

Using Python Pandas to Handle CSV Files | Python | XQA Learn