Home » excel » python – Appending a Pandas .read_excel dataframe to a new dataframe

python – Appending a Pandas .read_excel dataframe to a new dataframe

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to read several XLS files into a Panda’s dataframe. They appear to read in correctly – but when I trying and display(df), df.info() or df.head() the dataframe is empty.

All data is in subfolder named data1. xls sheets are named a.xls and b.xls.

The data sets are as follows:

a.xls

Account Description Final   Adjustment  Adj Final
324     Testing1    2342             0   2342
232     Essential   5678             0   5678
35      Options     2345             0   2345
634     Working       45          5675   5720
23      Time       45774             0  45774
52      Styling       67             0     67

b.xls

Account Description Final   Adjustment  Adj Final
343     Visual        345            0    345
576     IO Tools    34543            0  34543
6567    Options      6356            0   6356
8675    Pickling     7456         5675  13131
345     Clipboard    5678            0   5678
6365    Flat         2355          645   3000

Code:

import pandas as pd
import os
from IPython.display import display, HTML

df = pd.DataFrame()

for filename in os.listdir('data1'):
    if filename.endswith(".xls"):
        print(f'appending {filename}')
        data = pd.read_excel(os.path.join("data1", filename), sheet_name=0)
        display(data)
        df.append(data)
        display(df)
    else:
        print(f'skipping {filename}, wrong data type')

The Output:

    appending a.xls
       Account  Description Final   Adjustment  Adj Final
    0      324     Testing1  2342            0       2342
    1      232    Essential  5678            0       5678
    2       35      Options  2345            0       2345
    3      634      Working    45         5675       5720
    4       23         Time 45774            0      45774
    5       52      Styling    67            0         67
    ___

    appending b.xls
       Account  Description Final   Adjustment  Adj Final
    0      343       Visual   345            0        345
    1      576     IO Tools 34543            0      34543
    2     6567      Options  6356            0       6356
    3     8675     Pickling  7456         5675      13131
    4      345    Clipboard  5678            0       5678
    5     6365         Flat  2355          645       3000
    ___

The display(data) produces the tables above, but the display(df) produces the ___. So, somehow I’m reading the excel file into data, but it isn’t appending it to the df.

df.info() produces:

<class 'pandas.core.frame.DataFrame'>
Index: 0 entries
Empty DataFrame

My question is how to append the data into a single dataframe (df) for multiple excel workbooks.

How to&Answers:

You never reassign the variable df, then your dataframe is empty. You should do:

df = df.append(data)