Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Convert CSV to Excel using Pandas in Python?
In this article, we will show you how to convert a CSV file (Comma Separated Values) to an Excel file using the pandas module in Python. This is useful when you need to transform data into Excel format for reporting or sharing purposes.
We'll use a sample CSV file called ExampleCsvFile.csv containing cricket player data to demonstrate the conversion process.
Sample Data
Our example CSV file contains the following player statistics ?
| Player Name | Age | Type | Country | Team | Runs | Wickets |
|---|---|---|---|---|---|---|
| Virat Kohli | 33 | Batsman | India | Royal Challengers Bangalore | 6300 | 20 |
| Bhuvneshwar Kumar | 34 | Bowler | India | Sun Risers Hyderabad | 333 | 140 |
| MS Dhoni | 39 | Wicket Keeper | India | Chennai Super Kings | 4500 | 0 |
| Rashid Khan | 28 | Bowler | Afghanistan | Gujarat Titans | 500 | 130 |
Method 1: Converting CSV to Excel Without Index
This method creates an Excel file without displaying row index numbers ?
import pandas as pd
# Create sample CSV data for demonstration
data = {
'Player Name': ['Virat Kohli', 'Bhuvneshwar Kumar', 'MS Dhoni', 'Rashid Khan'],
'Age': [33, 34, 39, 28],
'Type': ['Batsman', 'Bowler', 'Wicket Keeper', 'Bowler'],
'Country': ['India', 'India', 'India', 'Afghanistan'],
'Team': ['RCB', 'SRH', 'CSK', 'GT'],
'Runs': [6300, 333, 4500, 500],
'Wickets': [20, 140, 0, 130]
}
# Create DataFrame and save as CSV
df = pd.DataFrame(data)
df.to_csv('sample_players.csv', index=False)
# Read the CSV file
csv_data = pd.read_csv('sample_players.csv')
# Convert to Excel without index
with pd.ExcelWriter('output_no_index.xlsx') as writer:
csv_data.to_excel(writer, index=False)
print("CSV converted to Excel successfully (without index)")
print("First 3 rows of the data:")
print(csv_data.head(3))
CSV converted to Excel successfully (without index)
First 3 rows of the data:
Player Name Age Type Country Team Runs Wickets
0 Virat Kohli 33 Batsman India RCB 6300 20
1 Bhuvneshwar Kumar 34 Bowler India SRH 333 140
2 MS Dhoni 39 Wicket Keeper India CSK 4500 0
Method 2: Converting CSV to Excel With Index
This method includes row index numbers in the Excel file ?
import pandas as pd
# Read CSV data
csv_data = pd.read_csv('sample_players.csv')
# Convert to Excel with index
with pd.ExcelWriter('output_with_index.xlsx') as writer:
csv_data.to_excel(writer, index=True)
# Read the Excel file back to verify
excel_data = pd.read_excel('output_with_index.xlsx')
print("CSV converted to Excel successfully (with index)")
print("Excel file contents:")
print(excel_data)
CSV converted to Excel successfully (with index) Excel file contents: Unnamed: 0 Player Name Age Type Country Team Runs Wickets 0 0 Virat Kohli 33 Batsman India RCB 6300 20 1 1 Bhuvneshwar Kumar 34 Bowler India SRH 333 140 2 2 MS Dhoni 39 Wicket Keeper India CSK 4500 0 3 3 Rashid Khan 28 Bowler Afghanistan GT 500 130
Using to_excel() Directly
You can also convert CSV to Excel directly using the to_excel() method ?
import pandas as pd
# Read CSV and convert directly to Excel
df = pd.read_csv('sample_players.csv')
df.to_excel('direct_output.xlsx', index=False, sheet_name='Players')
print("Direct conversion completed")
print(f"DataFrame shape: {df.shape}")
print(f"Columns: {list(df.columns)}")
Direct conversion completed DataFrame shape: (4, 7) Columns: ['Player Name', 'Age', 'Type', 'Country', 'Team', 'Runs', 'Wickets']
Key Parameters
| Parameter | Description | Example |
|---|---|---|
index |
Include row indices | index=False |
sheet_name |
Name of Excel sheet | sheet_name='Data' |
engine |
Excel writer engine | engine='openpyxl' |
Conclusion
Converting CSV to Excel with pandas is straightforward using to_excel(). Use index=False to exclude row numbers or index=True to include them. The ExcelWriter class provides more control for complex operations.
