I have a data set that looks like this (at most 5 columns - but can be less)
1,2,3
1,2,3,4
1,2,3,4,5
1,2
1,2,3,4
....
I am trying to use pandas read_table to read this into a 5 column data frame. I would like to read this in without additional massaging.
If I try
import pandas as pd
my_cols=['A','B','C','D','E']
my_df=pd.read_table(path,sep=',',header=None,names=my_cols)
I get an error - "column names have 5 fields, data has 3 fields".
Is there any way to make pandas fill in NaN for the missing columns while reading the data?
I'd also be interested to know if this is possible, from the doc it doesn't seem to be the case. What you could probably do is read the file line by line, and concatenate each reading to a DataFrame:
It works but not in the most elegant way, I guess...
One way which seems to work (at least in 0.10.1 and 0.11.0.dev-fc8de6d):
Note that this approach requires that you give names to the columns you want, though. Not as general as some other ways, but works well enough when it applies.
Ok. Not sure how efficient this is - but here is what I have done. Would love to hear if there is a better way to do this. Thanks !