How to Convert XLSX to CSV in Python Using Pandas
Working with Excel files (.xlsx
) is common in data analysis, but sometimes you need a simpler format like CSV (Comma-Separated Values). Python’s pandas library makes this conversion quick and easy. In this guide, we’ll walk you through the process step by step.
Prerequisites
Before you start, ensure you have the following installed:
- Python 3.6+ (recommended)
- pandas library (
pip install pandas
) - openpyxl (for reading Excel files, install via
pip install openpyxl
)
Step-by-Step Conversion
1. Import Pandas
First, import the pandas library in your Python script:
import pandas as pd
2. Read the Excel File
Use pd.read_excel()
to load the XLSX file into a DataFrame:
df = pd.read_excel('input_file.xlsx')
Replace 'input_file.xlsx'
with your file path.
3. Save as CSV
Convert the DataFrame to CSV using to_csv()
:
df.to_csv('output_file.csv', index=False)
Setting index=False
prevents pandas from adding an extra index column.
Handling Multiple Sheets
If your Excel file has multiple sheets, specify the sheet name or index:
df = pd.read_excel('input_file.xlsx', sheet_name='Sheet1')
Complete Example
Here’s a full script to convert XLSX to CSV:
import pandas as pd
# Read Excel file
df = pd.read_excel('data.xlsx')
# Save as CSV
df.to_csv('data.csv', index=False)
print("Conversion successful!")
- How to convert Excel to CSV using Python pandas
- Best way to convert XLSX to CSV in Python
- Python script to export Excel to CSV
- Convert multiple Excel sheets to CSV pandas
- Pandas read Excel and save as CSV
- Automate Excel to CSV conversion Python
- How to extract data from XLSX to CSV
- Python pandas XLSX to CSV without index
- Batch convert Excel files to CSV Python
- Read and write Excel files in Python
No comments:
Post a Comment