How to read the first row of a Dataframe as data row and not header

Viewed 1040

I'm trying to read the data out of pdf using Tabula-Py but facing a small issue:

The library reads the data like this:

  F 01/02/1994 01/12/1994 ac $123
0 M 01/03/1994 01/10/1992 ad $133
1 M 01/04/1994 01/09/1992 ae $133
2 M 01/05/1994 01/08/1992 aa $133

When I give them column names: The issue is that the first row is automatically being treated as Header and not the data while being Read by Tabula-PDF, so the output becomes:

  Gender  D.O.B       D.O.P      Plan  Price
0 M      01/03/1994  01/10/1992  ad    $133
1 M      01/04/1994  01/09/1992  ae    $133
2 M      01/05/1994  01/08/1992  aa    $133

which is incorrect. It drops the first row

The correct Output required is Below:

 Gender D.O.B       D.O.P      Plan Price
0 F     01/02/1994  01/12/1994 cc   $123
1 M     01/03/1994  01/10/1992 aa   $133
2 M     01/04/1994  01/09/1992 aa   $133
3 M     01/05/1994  01/08/1992 aa   $133

The code that i'm using is :

def table_type_2(path):
    df = read_pdf(path,guess = True, multiple_tables=True)
    head = ['Gender', 'Date of Birth', 'Date of', 'Coverage', 'Gross Benefit']

    for i in range (0,len(df)):
        print(len(df[i]))
        dfnew.append(df[i])

    e = pd.DataFrame(dfnew[i],index=None)
    print(e)
    f = pd.DataFrame(e,columns=head)
    g = pd.DataFrame(data = e.values,columns=f.columns)
    print(g) 

Is there a way so that i don't lose the first row well?

2 Answers

You can transpose the dataframe and reset_index so that the header becomes a normal column. Then you can transpose back:

def table_type_2(path):
    df = read_pdf(path,guess = True, multiple_tables=True)
    head = ['Gender', 'Date of Birth', 'Date of', 'Coverage', 'Gross Benefit']

    df = df.T.reset_index().T.reset_index(drop=True)

    df.columns = head 

    return df
Related