![](/img/trans.png)
[英]How to read & combine specific rows from multiple excel files into a single file using python?
[英]How to combine all rows and columns of an excel file into a single cell of another excel file using python?
我有一個 Excel 'sample1.xlsx',其中包含以下詳細信息:-
0 1 2
0 Name Address Phone No.
1 abc 22/2 0154235
2 xyz 12-3 9832033
我正在尋找一種使用 Pandas 數據框從上述 excel 文件中刪除標題的實現:-
0 1 2
0 abc 22/2 0154235
1 xyz 12-3 9832033
此后,將所有行和列組合到一個單元格中,元素之間有一個空格,並將輸出寫入另一個 excel 文件:-
0
0 abc 22/2 0154235 xyz 12-3 9832033
任何人都可以幫助我進行上述實施嗎?
我已經實現了以下用於刪除標題的代碼,
import pandas
excel_data_df = pandas.read_excel('sample.xlsx', sheet_name='Sheet1')
df = excel_data_df.iloc[3:]
看起來您不需要讀取標題header=None
並使用df.stack()
來堆疊數據幀並轉回:
pd.read_excel('file.xlsx',header=None,
skiprows=[0]).stack().to_frame().reset_index(drop=True).T
同樣使用df.to_numpy()
轉換為 np 數組並使用np.flatten()
我們可以創建一個數據np.flatten()
並轉置:
pd.DataFrame(pd.read_excel('file.xlsx').to_numpy().flatten()).T
0 1 2 3 4 5
0 abc 22/2 154235 xyz 12-3 9832033
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.