0

My csv file looks like this:

5783,145v
g656,4589,3243,tt56
6579

How do I read this with pandas (or otherwise)?

(the table should contain empty cells)

user1581390
  • 1,900
  • 5
  • 25
  • 38
  • Does this answer your question? [Handling Variable Number of Columns with Pandas - Python](https://stackoverflow.com/questions/15242746/handling-variable-number-of-columns-with-pandas-python) – Gino Mempin Mar 26 '20 at 00:17

3 Answers3

2

You could pass a dummy separator, and then use str.split (by ",") with expand=True:

df = pd.read_csv('path/to/file.csv', sep=" ", header=None)
df = df[0].str.split(",", expand=True).fillna("")
print(df)

Output

      0     1     2     3
0  5783  145v            
1  g656  4589  3243  tt56
2  6579                  
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
1

I think that the solution proposed by @researchnewbie is good. If you need to replace the NaN values for say, zero, you could add this line after the read:

dataFrame.fillna(0, inplace=True)
Stein
  • 719
  • 7
  • 9
  • You can also do dataFrame.replace(NaN, 0). The first argument being what you want to replace in the entire dataFrame and the second what you want to replace it with. If they want an empty string, 0, etc. – researchnewbie Oct 20 '19 at 02:30
0

Try doing the following:

import pandas as pd

dataFrame = pd.read_csv(filename)

Your empty cells should contain the NaN value, which essentially null.

researchnewbie
  • 100
  • 1
  • 10