Python Pandas: Excel to CSV Conversion Guide
This guide demonstrates how to convert data from an Excel spreadsheet into a CSV file using Python's Pandas library.
Prerequisites:
- Python installed on your system.
- Pandas library installed. If not, use the following command in your terminal:
pip install pandas
Code Implementation:
import pandas as pd
# Read Excel file
df = pd.read_excel('input.xlsx')
# Save data as CSV
df.to_csv('output.csv', index=False)
Explanation:
- Import Pandas:
import pandas as pdimports the Pandas library, allowing you to work with data structures like DataFrames. - Read Excel File:
df = pd.read_excel('input.xlsx')reads the Excel file named 'input.xlsx' and stores the data in a Pandas DataFrame named 'df'. - Save to CSV:
df.to_csv('output.csv', index=False)saves the DataFrame 'df' as a CSV file named 'output.csv'. Theindex=Falseargument ensures that row indices are not included in the CSV file. To include indices, change this toindex=True.
Key Points:
- Replace 'input.xlsx' and 'output.csv' with the actual file paths for your Excel and CSV files.
- This code assumes your Excel file has a
.xlsxextension. If it's a different format (e.g.,.xls), adjust the file path accordingly. - The
index=Falseparameter is crucial to prevent the row indices from appearing in the resulting CSV file. This ensures compatibility with various data analysis tools and applications.
By following these steps, you can effortlessly convert Excel data into a convenient CSV format using Python's Pandas library, making data manipulation and analysis even more efficient.
原文地址: https://www.cveoy.top/t/topic/pcFK 著作权归作者所有。请勿转载和采集!