Skip to content
Advertisement

Python reads csv in just one column

So im am relativley new to python (using Python 3 with the spyder IDE) and i try to read in a csv file with some weather data.enter image description here The problem is that the file i have contains some empty cells and information i dont need. I only need from the row 18 as a header (all the physical descriptions of the data e.g. Temperature, pressure etc are stored in this row) and all columns. When i try the following in python:

import pandas as pd

import numpy as np

df = pd.read_csv(r'C:UserstroesOneDriveDokumentestudium8 SemesterBATcode_transferVNYA16.201812160000.csv')

df = df[16:91]

Python stores all of the data in just one column like this enter image description here

But the output i want is a dataframe which contains in the first column the DATE_TIME, second LEAD_TIME, third T_2M_K and so on.

Does anybody know how to fix that properly? enter image description here enter image description here

Advertisement

Answer

This might help you move in the right direction:

pd.read_csv() can take some extra arguments. See Pandas Doc here: pandas.read_csv.

Firstly, since you have extra rows before the header, add the header argument to specify the row. e.g

pd.read_csv(path_to_file, header = 17) 

The default seperator in read_csv() is ‘,’ whereas it looks like your seperator is ‘;’. I would therefore also add the seperator argument. e.g:

pd.read_csv(path_to_file, header=17, sep=';') 

Hopefully, that gets you something more reasonable.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement