Importing files and saving
- to import an excel file, use the following code in the IDLE window:
- if there is no header, use the following code:
- use 'df.head(n)' or 'df.tail(n)' to show only 'n' number of rows from the start/end
- to give names to headers, use the following example:
- to save the revised file, use the code:
('index=False' is used to prevent saving the DataFrame index as a separate column in the output file)
df.to_excel("data.xlsx", index=False)
- if you don't specify the file path, the file will be saved in the 'current working directory'. To find out the current working directory, use the following code:
- to change the 'current working directory' run the following code:
import pandas as pd
df = pd.read_excel("C:/Users/Owner/Desktop/file_name.xlsx")
print(df)
df = pd.read_excel('data.xlsx', header=None)
headers = ["Col1_Name", "Col2_Name"]
df.columns = headers
print(df)
import os
current_directory = os.getcwd()
print("Current working directory:", current_directory)
import os
# Specify the directory path you want to change to
new_directory = "/path/to/new/directory"
# Change the current working directory
os.chdir(new_directory)
# Verify that the current working directory has been changed
current_directory = os.getcwd()
print ("Current working directory:", current_directory)