How to parse excel file with multiple tables in one sheet using Pandas

Viewed 29

I have a table on the following format after reading the excel file with Pandas

 Unnamed: 0   |   Unnamed: 1  |   Unnamed: 2  |   .....   | Unnamed: 10  |
 RandomText          NaN              NaN                        NaN          
 NameTable1          NaN              NaN                        NaN       
    NaN        Header1Table1    Header2Table1               Header10Table1
    NaN        Value1Table1     Value2Table1                 Value10Table1
    ...             ...             ...                          ...
 RandomText          NaN              NaN                        NaN          
 NameTable2          NaN              NaN                        NaN       
    NaN        Header1Table2    Header2Table2                    NaN
    NaN        Value1Table2     Value2Table2                     NaN
    ...             ...             ...                          ...
 

The logic for how the file is

  • Column Unnamed: 0 will contain information about the table. There will be two consecutive rows before a table, the second row describes the upcoming table name. Otherwise all values in Unnamed: 0 is NaN.
  • The next row is where the Table header starts.
  • The table might have NaN values.
  • The size of the tables can differ.
  • The last row of a Table will be the row before the next non NaN value in column Unnamed: 0.
  • The headers of the different tables might differ in name and size.

Is there a way to parse this file into a single DataFrame, where we add an additional column indicating the table name? Or if not possible, split it into different tables that share the same headers?

0 Answers
Related