我需要將 2 列從 Excel 檔案“Input.xlsx”復制到新的 Excel 檔案“Output.xlsx”。我使用 Pandas 嘗試了以下代碼,但在“Output.xlsx”檔案中,資料全部寫入第一行,我不再有 2 列資料。你能幫我修復代碼嗎?這是代碼:
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)
uj5u.com熱心網友回復:
對于單個檔案:
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)
對于多個檔案:
import pandas as pd
fns = ['Input1.xlsx', 'Input2.xlsx']
column_names = [' Speed of sound (c) [m/s]',' Outlet Temperature (T out) [°C]']
input_dfs = []
for fn in fns:
input_df = pd.read_excel(fn, usecols=column_names)
input_dfs.append(input_df)
output_df = pd.concat(input_dfs)
output_df.to_excel('Output.xlsx', index=False)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/409590.html
標籤:
