user308827
user308827

Reputation: 21971

Reading in text file into pandas dataframe failing

I have the foll. input file:

  1988   1   1  7.88-15.57-25.00  0.00  0.81  4.02
  1988   1   2  6.50-10.37-24.87  0.00  0.49  4.30
  1988   1   3  6.48 -8.79-21.28  0.00  0.62  3.91

and I read it as follows:

df = pandas.read_csv(inp_file, header=None, sep=' ')

However, because of no spaces present between some columns, they are not getting read correctly. Is there a way I can specify individual column widths?

Upvotes: 1

Views: 155

Answers (1)

EdChum
EdChum

Reputation: 394051

OK, read_fwf works I thought your 3rd line was malformed but it looks pukka:

In [9]:

t="""1988   1   1  7.88-15.57-25.00  0.00  0.81  4.02
1988   1   2  6.50-10.37-24.87  0.00  0.49  4.30
1988   1   3  6.48 -8.79-21.28  0.00  0.62  3.91"""
pd.read_fwf(io.StringIO(t),header=None)
Out[9]:
      0  1  2                 3  4     5     6
0  1988  1  1  7.88-15.57-25.00  0  0.81  4.02
1  1988  1  2  6.50-10.37-24.87  0  0.49  4.30
2  1988  1  3  6.48 -8.79-21.28  0  0.62  3.91

Upvotes: 1

Related Questions