Tuesday, 17 September 2013

read csv with initial white space in pandas

read csv with initial white space in pandas

I have a problem in pandas with a csv file that has initial whitespace in
some columns, for example:
pd.read_csv(StringIO("""1 2\n 3 4\n5 6"""), delim_whitespace=True,
names=["a", "b"], skipinitialspace=True)
a b
0 1 2
1 NaN 3
2 5 6
I tried using instead \s+ regular expression as separator and it works as
I expect:
pd.read_csv(StringIO("""1 2\n 3 4\n5 6"""), sep="\s+", names=["a", "b"])
a b
0 1 2
1 3 4
2 5 6
However, I would like to understand why skipinitialspace is not solving
the issue in the first case.

No comments:

Post a Comment