Empty column in Pandas Dataframe

How to ignore trailing spaces in a string when converting to Pandas DataFrame?

I have a CSV file in the following format:

Column #1   : Type
Column #2   : Total Length
Column #3   : Found
Column #4   : Grand Total

1;2;1;7.00;
2;32;2;0.76;
3;4;6;6.00;
4;1;5;4.00;

      

I loop through the "Column #" lines to create the column names first (so 4 columns), then I parse the following lines to create my DataFrame with ';' as a separator. However, some of my files contain a trailing ';' at the end of each row as shown above, so my Pandas DataFrame thinks there is a 5th column containing spaces and hence throws an error to say there are not enough column names.

Is there a mechanism in Pandas to remove / ignore the trailing ';' or a space when creating a DataFrame? I am using read_csv to create a DataFrame.

Thank.

+3


source to share


1 answer


Just pass the parameter to usecols

:

In [160]:
t="""1;2;1;7.00;
2;32;2;0.76;
3;4;6;6.00;
4;1;5;4.00;"""
โ€‹import pandas as pd
import io
df = pd.read_csv(io.StringIO(t), sep=';', header=None, usecols=range(4))
df

Out[160]:
   0   1  2     3
0  1   2  1  7.00
1  2  32  2  0.76
2  3   4  6  6.00
3  4   1  5  4.00

      



Here I am creating a list [0,1,2,3]

to indicate which columns I am interested in.

+1


source







All Articles