Skip to main content

CSV Files Handling

CSV Files Handling in Python

CSV (Comma Separated Values) files are one of the most common data formats used in data science, machine learning, and analytics. Python is a powerful programming language that provides several tools and libraries to work with CSV files. In this article, we will explore the basics of working with CSV files in Python, including reading, writing, and manipulating data. We will also cover some advanced topics, such as handling large CSV files, dealing with missing data, and performing operations on CSV data using NumPy and Pandas libraries.

Open CSV File and Read Data with Python

To open and read a CSV file in Python, you can use the built-in csv module.

import csv

with open('example.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

In this example, we use the csv.reader() function to read the contents of the CSV file named example.csv. We then loop through the rows of the file using a for loop and print each row to the console.

import csv

with open('example.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Age', 'Gender'])
    writer.writerow(['John', '25', 'Male'])
    writer.writerow(['Jane', '30', 'Female'])

In this example, we use the csv.writer() function to write data to a CSV file named example.csv. We create a new file with the w mode and specify newline='' to avoid extra line breaks. We then use the writerow() function to write each row of data to the file.

By using these code examples, you can easily provide CSV reading or loading CSV.

How to Save to a CSV File in Python

Saving data in a CSV file is a common task in Python. CSV files are easy to read and can be easily opened in any spreadsheet software. In Python, we can use the csv module to write to a CSV file. Here are a few examples of how to save to a CSV file in Python.

This example demonstrates how to write a simple list of values to a CSV file.

import csv

# Example data
data = [['Name', 'Age', 'Gender'], ['Alice', '25', 'Female'], ['Bob', '30', 'Male'], ['Charlie', '35', 'Male']]

# Open csv file in write mode
with open('example.csv', mode='w') as file:
    writer = csv.writer(file)
    # Write data to csv file
    writer.writerows(data)

In the code above:

  1. We import the csv module.
  2. We create a simple list of values called data.
  3. We open the CSV file in write mode using the open() function and specify the mode as 'w'.
  4. We create a csv.writer object and pass the file object to the writer.
  5. We use the writerows() method to write the data to the CSV file.

This example shows how to write a dictionary of values to a CSV file.

import csv

# Example data
data = [{'Name': 'Alice', 'Age': '25', 'Gender': 'Female'},
        {'Name': 'Bob', 'Age': '30', 'Gender': 'Male'},
        {'Name': 'Charlie', 'Age': '35', 'Gender': 'Male'}]

# Open csv file in write mode
with open('example.csv', mode='w', newline='') as file:
    fieldnames = ['Name', 'Age', 'Gender']
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    # Write data to csv file
    for item in data:
        writer.writerow(item)

In the code above:

  1. We import the csv module.
  2. We create a list of dictionaries called data.
  3. We open the CSV file in write mode using the open() function and specify the mode as 'w'. We also set newline to '' to prevent blank rows from being inserted between each row.
  4. We create a csv.DictWriter object and pass the file object to the writer. We also provide the fieldnames as a list.
  5. We use writeheader() method to write the fieldnames to the CSV file.
  6. We use the writerow() method to write each row of data to the CSV file.

By using the csv module in Python, you can easily save your data to a CSV file. These examples can be modified to meet your specific requirements.

How to Convert JSON to CSV with Python

Converting json data to CSV format is a common task in data processing. Python offers an easy and efficient way to convert JSON data to CSV format using built-in modules such as json and csv.

Using JSON and CSV modules

import json
import csv

# Load JSON data
with open('data.json', 'r') as file:
    data = json.load(file)

# Open CSV file for writing
with open('data.csv', 'w', newline='') as file:
    writer = csv.writer(file)

    # Write header row
    writer.writerow(data[0].keys())

    # Write data rows
    for item in data:
        writer.writerow(item.values())

Using Pandas Library

import pandas as pd

# Load JSON data
with open('data.json', 'r') as file:
    data = json.load(file)

# Convert to dataframe
df = pd.DataFrame(data)

# Write to CSV file
df.to_csv('data.csv', index=False)

In both of these examples, we load the JSON data from a file, convert it to a Python object, and then write it to a CSV file using the csv module or pandas library. With these methods, you can easily convert JSON data to CSV format in Python.

Read CSV with Pandas

Pandas is a powerful open-source data analysis library for Python that offers easy-to-use data structures for data manipulation and analysis. In pandas, reading and manipulating CSV files is simple and efficient.

Load CSV with Pandas

To load a CSV file with Pandas, we use read_csv(). Let's see how we can load a CSV file using Pandas:

import pandas as pd
df = pd.read_csv('filename.csv')
print(df.head())

Parse CSV File using Pandas

After loading the CSV file, we need to parse the data to extract the required information. Pandas provides a lot of operations to parse and manipulate CSV data. Here's an example of how to parse data using Pandas:

import pandas as pd
df = pd.read_csv('filename.csv')
df = df[df['column_name'] == 'required_value']
print(df.head())

Write DataFrame to CSV using Pandas

After processing the CSV data, we may want to write the new DataFrame to a new CSV file. Pandas provides an easy way to write the DataFrame to CSV files using to_csv(). Here's an example:

import pandas as pd
df = pd.read_csv('filename.csv')
# Perform operations to extract the required data
new_df = df[df['column_name'] == 'required_value']
# Write the new DataFrame to a new CSV file
new_df.to_csv('new_file.csv', index=False)

Export to CSV

Exporting data to CSV (Comma Separated Values) is a common task in data processing. Here are two ways to export data to CSV in Python:

Using csv module

The csv module is a built-in module in Python that enables reading and writing of CSV files. Here's an example of exporting a dictionary to a CSV file using the csv module:

import csv

data = {'name': ['John', 'Jane', 'Adam'], 'age': [20, 25, 30]}

with open('data.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(data.keys())
    writer.writerows(zip(*data.values()))

# This code creates a CSV file with the following format:
# 
# 
# name,age
# John,20
# Jane,25
# Adam,30

Using pandas module

Here's an example of exporting a pandas DataFrame to a CSV file.

import pandas as pd

data = {'name': ['John', 'Jane', 'Adam'], 'age': [20, 25, 30]}
df = pd.DataFrame(data)

df.to_csv('data.csv', index=False)

This code creates a CSV file with the same format as the previous example. The index=False parameter is used to remove the default row index column from the CSV file.

Read CSV Line by Line

To read a CSV file in Python line by line, we can use the built-in csv.

Reading CSV Line by Line

import csv

with open('example.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)

In the above example, we open the CSV file example.csv and assign it to the csvfile variable. Then we create a csv.reader object, which we can iterate over line by line using a for loop. Each row in the loop is represented as a list of values.

Writing to New Line in CSV

import csv

with open('example.csv', mode='a', newline='') as csvfile:
    writer = csv.writer(csvfile)
    row = ['value1', 'value2', 'value3']
    writer.writerow(row)

In the above example, we open the CSV file example.csv in 'append' mode and assign it to the csvfile variable. Then we create a csv.writer object, which we can use to write a new line to the CSV file using the writerow() method. The row variable is a list of values to write to the new line in the CSV file.

By using these simple examples, we can easily read and write to CSV files line by line in Python.

How to Read one Column CSV in Python

To read one column CSV in Python, you can use the csv.DictReader() function to read CSV files as dictionaries. Here are two examples:

import csv

with open('example.csv') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row['column_name'])

In this code example, we first import the csv module. We then use the with statement to open the CSV file example.csv. We create a DictReader object called reader using the CSV file file. We then iterate through each row in reader and print the value of column_name in each row.

import pandas as pd

data = pd.read_csv('example.csv')
column_data = data['column_name']
print(column_data)

In this code example, we first import the pandas module and create a DataFrame called data using the read_csv() function and passing the CSV file name example.csv. We then assign the data in column_name to a new variable column_data. Finally, we print column_data.

Contribute with us!

Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.

Profile picture for user AliaksandrSumich
Python engineer, expert in third-party web services integration.
Updated: 05/03/2024 - 21:52
Profile picture for user angarsky
Reviewed and approved