4

I have a csv file with one row of data with no header. Below is my code for importing data into a dataframe:

df2 = pd.read_csv(path2, header=0)

When I do read_csv it returns the following:

Empty DataFrame
Columns: [0.940456, 0.077893, 0.840178, 0.668612, 0.923643, 0.641833, 0.845249, 0.361605, 0.453943, 0.695509, 0.825763, 0.503687, 0.617303, 0.276637, 0.636244, 0.075744]
Index: []

df2.info() returns the following:

Index: 0 entries
Data columns (total 16 columns):
0.940456    0 non-null object
...

How do I set the first row as row instead of columns?

user3088202
  • 2,034
  • 3
  • 21
  • 34

1 Answers1

6

It means first row is converted to columns names, need header=None for default columns names with data in first row of DataFrame:

df2 = pd.read_csv(path2, header=None)
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
  • 1
    Thank you. Dont know I missed this. – user3088202 Feb 06 '19 at 14:42
  • 1
    The default behavior for `pd.read_csv()` is `header=0` which means that the first line will be considered as column names while `header=None` will not take the first row as columns as indicated in [the documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html). – Tiphaine Feb 06 '19 at 14:45
  • @jezrael: Could you please look into my question: https://stackoverflow.com/questions/72490042/pandas-empty-dataframe-when-reading-from-stringio-with-read-csv – Benison Sam Jun 03 '22 at 13:06