1

Hello I have CSV file which has no header while reading I am getting error the data CSV look as below

 School1,std1,std2
 Schoo2,std3,std4,std5,std6,std7
 School4,std1,std6
 School6,std9,std10

Because of incomplete column not able read

df=of.read_csv("test.txt",sep=",", header=None)

Can any one suggest me how can I read this file

BISEP
  • 11
  • 1
  • These type or arrays are often referred to as "ragged", wich that search term you can find what you need already answered here https://stackoverflow.com/questions/46127026/handling-ragged-csv-columns-in-pandas – Mikael Öhman Apr 13 '21 at 21:42

1 Answers1

0

If you know the number of columns of your largest row, you can just create a list to be the header, as for your example, index 1 has 6 columns, so:

col_names = [1,2,3,4,5,6]
df = pd.read_csv("test.txt",sep=",", header=None, names=col_names)

If you're handling with a huge amount of rows and don't know the max number of columns, you can check this topic for a better solution: import csv with different number of columns per row using Pandas

dmildem
  • 25
  • 5