Skip to content Skip to sidebar Skip to footer

How To Read In Pretty-printed Dataframe Into A Pandas Dataframe?

# necessary imports from tabulate import tabulate import pandas as pd I have a dataframe: df = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', '

Solution 1:

One solution is to use clever keyword arguments in pd.read_csv / pd.read_clipboard:

    df = pd.read_csv(r'PrettyPrintOutput.txt', sep='|', comment='+', skiprows=[2], index_col=1)
    df = df[[col for col in df.columns if'Unnamed'notin col]]

I just define all lines beginning with '+' as comments, so they don't get imported. This does not help against the third row, which has to be excluded using skiprow.

The second line is needed because you end up with additional columns using the '|' as separator. If you know the column names in advance use the keyword usecols to be explicit.

Output:

AB      C      D   
                                
0      A0     B0     C0     D0  
1      A1     B1     C1     D1  
2      A2     B2     C2     D2  
3      A3     B3     C3     D3 

It also works with pd.read_clipboard, because the functions accept the same keyword arguments.

Post a Comment for "How To Read In Pretty-printed Dataframe Into A Pandas Dataframe?"