baxx
baxx

Reputation: 4705

How to read a csv in pandas with a missing delimiter (or - with additional delimiters)

import pandas as pd
from io import StringIO

csv_one = """\
one;two;three
1;2;3;
4;5;6;
"""
df1 = pd.read_csv(StringIO(csv), sep=";")

This data looks as:

   one  two  three
1    2    3    NaN
4    5    6    NaN

The desired result is:

   one  two  three
    1    2    3    
    4    5    6    

I could manually edit the csv, but I don't really want to have to do that if possible.

In R the function read_delim can manage this with something along the lines of read_delim( <path>, ";", escape_double = FALSE, trim_ws = TRUE)

Upvotes: 2

Views: 598

Answers (1)

jezrael
jezrael

Reputation: 862661

For me working index_col=False parameter:

csv = """\
one;two;three
1;2;3;
4;5;6;
"""
df1 = pd.read_csv(StringIO(csv), sep=";", index_col=False)
print (df1)
   one  two  three
0    1    2      3
1    4    5      6

Upvotes: 1

Related Questions