Convert CSV to Excel using Pandas in Python

Convert CSV to Excel using Pandas in Python

Using pandas, it's straightforward to convert a CSV file to an Excel file. Here's how you can do it:

Step-by-step:

  1. First, you need to install pandas and openpyxl (used as the default Excel writer by pandas):

    pip install pandas openpyxl 
  2. Here's a simple Python script to convert a CSV file to an Excel file:

import pandas as pd # Load the CSV file into a DataFrame df = pd.read_csv('path_to_input.csv') # Write the DataFrame to an Excel file df.to_excel('path_to_output.xlsx', index=False, engine='openpyxl') 

Replace path_to_input.csv with the path to your CSV file and path_to_output.xlsx with the desired output path for the Excel file.

The index=False argument ensures that the DataFrame's index is not written to the Excel file. If you want the index to be written, you can omit this argument.

The engine='openpyxl' argument specifies the writer engine to use, and in this case, we're using the openpyxl engine, which is capable of writing .xlsx files.


More Tags

eventhandler ngmodel flutter-listview release fs jasmine angularjs-service google-apps-script decision-tree drop

More Programming Guides

Other Guides

More Programming Examples