简体   繁体   English

如何使用python将excel文件的所有行和列组合到另一个excel文件的单个单元格中?

[英]How to combine all rows and columns of an excel file into a single cell of another excel file using python?

I have an Excel 'sample1.xlsx' which has the following details:-我有一个 Excel 'sample1.xlsx',其中包含以下详细信息:-

    0     1       2
0 Name  Address Phone No.
1  abc   22/2    0154235
2  xyz   12-3    9832033

I'm looking for an implementation to remove the header from the above excel file using pandas dataframe:-我正在寻找一种使用 Pandas 数据框从上述 excel 文件中删除标题的实现:-

   0     1       2
0 abc  22/2   0154235
1 xyz  12-3   9832033

Thereafter, combine all rows and columns to a single cell with a space in between the elements and write the output to another excel file:-此后,将所有行和列组合到一个单元格中,元素之间有一个空格,并将输出写入另一个 excel 文件:-

                  0
0 abc 22/2 0154235 xyz 12-3 9832033

Can anyone help me in the above implementation?任何人都可以帮助我进行上述实施吗?

I had already implemented the below code for removing the header,我已经实现了以下用于删除标题的代码,

import pandas
excel_data_df = pandas.read_excel('sample.xlsx', sheet_name='Sheet1')
df = excel_data_df.iloc[3:]

Looks like you need not read the headers header=None and using df.stack() to stack the dataframe and transpose back:看起来您不需要读取标题header=None并使用df.stack()来堆叠数据帧并转回:

pd.read_excel('file.xlsx',header=None,
    skiprows=[0]).stack().to_frame().reset_index(drop=True).T

Similarly using df.to_numpy() convert to np array and using np.flatten() we can create a dataframe and transpose:同样使用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.

相关问题 如何使用 python 从多个 excel 文件中读取和组合特定行到单个文件中? - How to read & combine specific rows from multiple excel files into a single file using python? 如何使用 python 将文本文件拆分为行和列并保存在 excel 中 - How to split the text file into rows and columns and save in excel using python 如何将 2 个 Excel 列使用 DataFrame 然后 output 与另一个 ZC1D81AF5835844B4EZDC 文件进行比较? - How to compare 2 Excel columns using DataFrame then output it to another Excel file? Python:使用Excel CSV文件只读取某些列和行 - Python: Using Excel CSV file to read only certain columns and rows 如何使用 python 将 Excel 文件中的所有工作表合并或合并到一个工作表中? - How can combine or merge all worksheets within an Excel file into one worksheet using python? 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 如何使用 python 将 2 列相似数据连接到具有相同列标题的同一 excel 文件中的单个列中 - How to join 2 columns of similar data into a single column in a same excel file with same column headings using python 在 Excel 文件中,如何使用 openpyxl (Python) 查找单元格的背景颜色 - In a Excel file how to find the background color of a cell using openpyxl (Python) 如何使用 python 在 Excel 单元格中显示嵌入的文本文件 - How to display embedded text file in Excel cell using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM