I need to copy 2 columns from Excel file 'Input.xlsx' to a new Excel file 'Output.xlsx'. I tried the following code using Pandas, but in the 'Output.xlsx' file data turn out written all in the first row, I do not have 2 columns of data anymore. Can you help me fixing the code? Here is the code:
import pandas as pd
document1 = pd.ExcelFile('C:\\Users\\PycharmProjects\\Input.xlsx')
sheets=document1.sheet_names
appended_data=[]
df1=pd.read_excel(document1, sheets, usecols=[' Speed of sound (c) [m/s]',' Outlet Temperature (T out) [°C]'])
appended_data.append(df1)
appended_data=pd.DataFrame(appended_data)
appended_data.to_excel('Output.xlsx', index=False)
CodePudding user response:
df1 is already a Pandas DataFrame. Have you tried skipping the appended data steps, i.e.
import pandas as pd
fn = 'C:\\Users\\PycharmProjects\\Input.xlsx'
df = pd.read_excel(fn, usecols=[' Speed of sound (c) [m/s]',' Outlet Temperature (T out) [°C]'])
df.to_excel('Output.xlsx', index=False)
