0

My textfile raw data looks like this:

 ID   x1    x2
 1    4.3   4.7
 2          3.2
 3    3     5.6
 4          2.1

There are double spaces in the text file where there is no x1.

Then when reading into pandas using:

 df = pd.read_csv(filename,delim_whitespace=True)
 #or trying
 df = pd.read_csv(thia_concat,  sep = "\s+")

creates a data frame like this:

 ID   x1    x2
 1    4.3   4.7
 2    3.2   nan
 3    3     5.6
 4    2.1   nan

I can't figure out how to tell pandas what to do with the double space to assign the value to x2 and not x1. Any suggestions?

closed as off-topic by DPSSpatial, user2856, Fezter Nov 3 '16 at 22:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Might want to try StackOverflow for this question... – DPSSpatial Nov 3 '16 at 19:40
2

Use read_table or read_csv without passing a separator/delimeter and let Pandas figure it out:

"Delimiter to use. If sep is None, will try to automatically determine this"

df = pd.read_table(filename)
df

     ID   x1    x2
0   1    4.3   4.7
1   2          3.2
2   3    3     5.6
3   4          2.1


df = pd.read_csv(filename)
df

     ID   x1    x2
0   1    4.3   4.7
1   2          3.2
2   3    3     5.6
3   4          2.1

Not the answer you're looking for? Browse other questions tagged or ask your own question.