Issue
I'm reading a file with numerical values.
data = pd.read_csv('data.dat', sep=' ', header=None)
In the text file, each row end with a space, So pandas wait for a value that is not there and add a "nan" at the end of each row. For example:
2.343 4.234
is read as: [2.343, 4.234, nan]
I can avoid it using , usecols = [0 1]
but I would prefer a more general solution
Solution
You can use regular expressions in your sep
argument.
Instead of specifying the separator to be one space, you can ask it to use as a separator any number of spaces until it finds the next value. You can do this by using the regular expression \s+
:
data = pd.read_csv('data.dat', sep='\s+', header=None)
Answered By - Harry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.