Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

python - Using pandas to read text file with leading whitespace gives a NaN column

I am using pandas.read_csv to read a whitespace delimited file. The file has a variable number of whitespace characters in front of every line (the numbers are right-aligned). When I read this file, it creates a column of NaN. Why does this happen, and what is the best way to prevent it?

Example:

Text file:

  9.0  3.3 4.0
 32.3 44.3 5.1
  7.2  1.1 0.9

Command:

import pandas as pd
pd.read_csv("test.txt",delim_whitespace=True,header=None)

Output:

    0     1     2    3
0 NaN   9.0   3.3  4.0
1 NaN  32.3  44.3  5.1
2 NaN   7.2   1.1  0.9
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FWIW I tend to use s+ instead, and it doesn't suffer the same problem:

>>> pd.read_csv("wspace.csv", header=None, delim_whitespace=True)
    0     1     2    3
0 NaN   9.0   3.3  4.0
1 NaN  32.3  44.3  5.1
2 NaN   7.2   1.1  0.9
>>> pd.read_csv("wspace.csv", header=None, sep=r"s+")
      0     1    2
0   9.0   3.3  4.0
1  32.3  44.3  5.1
2   7.2   1.1  0.9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...