Export Columns from CSV to Excel in Python with Visual Studio Code on Windows - DevPops-Inc/python GitHub Wiki
• Press the Windows key, type “visual studio code” and select “Open.”
•
• Press the Ctrl and N key to create a new file.
•
• Select “Select a Language.”
•
• Type “python” in the search box and select “Python (python)” from the result.
•
• Type import pandas as pd
and press the Enter key to import the Pandas module as pd variable.
•
• Type from openpyxl.workbook import Workbook
and press the EnterK key twice to import the Workbook
object from the openpyxl.workbook
module.
•
• 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.
•
• Type df.columns = [‘< column 1 >, ‘< column 2 >’, ‘< . . . >’]
and press the Enter key twice to define the list of columns.
•
• Type print(df.columns)
to print the columns in the TERMINAL.
•
• Press the Ctrl and S keys to save.
•
• Browse to the desired location, type the filename in the “File name:” box then select “Save.”
•
• Press the Run button to run the Python script.
•
• The columns will print in the TERMINAL.
•
• Type clear
and press the Enter key to clear the TERMINAL.
•
• 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.
•
• Type stored = wanted_values.to_excel(‘< filename >.xlsx’, index=None)
to export the desired columns to an Excel file.
•
• Press the Run button.
•
• Browse to the file location in the File Explorer and double-click the exported Excel file.
•
• The Excel file will open with the values of the columns that you wanted.
•