简体   繁体   中英

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:-

    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:-

   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:-

                  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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM