Skip to content

Export Columns from CSV to Excel in Python with Visual Studio Code on Windows

Vic Phan edited this page Apr 16, 2023 · 1 revision

• Press the Windows key, type “visual studio code” and select “Open.”
image

• Press the Ctrl and N key to create a new file.
image

• Select “Select a Language.”
image

• Type “python” in the search box and select “Python (python)” from the result.
image

• Type import pandas as pd and press the Enter key to import the Pandas module as pd variable.
image

• Type from openpyxl.workbook import Workbook and press the EnterK key twice to import the Workbook object from the openpyxl.workbook module.
image

• Type df = pd.read_csv(‘< filename >.csv’, header=None) and press the Enter key to declare the df variable and set it to read the .csv file without headers.
image

• Type df.columns = [‘< column 1 >, ‘< column 2 >’, ‘< . . . >’] and press the Enter key twice to define the list of columns.
image

• Type print(df.columns) to print the columns in the TERMINAL.
image

• Press the Ctrl and S keys to save.
image

• Browse to the desired location, type the filename in the “File name:” box then select “Save.”
image

• Press the Run button to run the Python script.
image

• The columns will print in the TERMINAL.
image

• Type clear and press the Enter key to clear the TERMINAL.
image

• Return to the editor, replace print(df.columns) with wanted_values = df[[‘< column 1 >, ‘< column 2 >’, ‘< . . . >’]] and press the Enter key to declare the wanted_values variable and set its value to the columns you wish to export to an Excel file.
image

• Type stored = wanted_values.to_excel(‘< filename >.xlsx’, index=None) to export the desired columns to an Excel file.
image

• Press the Run button.
image

• Browse to the file location in the File Explorer and double-click the exported Excel file.
image

• The Excel file will open with the values of the columns that you wanted.
image

Viccy GitHub

Clone this wiki locally