1

I have several csv files. Each csv file is present with its description spanning across several rows (15 rows in few files, 100 rows in few other etc.). I want to read the csv files into dataframes. I tried to use pandas.DataFrame('file1.csv') for reading data into dataframe. How ever, I am getting the following error.

Traceback (most recent call last):
  File "snowdepthData.py", line 5, in <module>
    depthDF = pd.DataFrame('Alaska_SD_Sep2019toOct2020.csv')
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 485, in __init__
    raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!

Is there any way, I can skip reading the description and convert data into dataframe.

Thankyou.

srinivas
  • 301
  • 1
  • 9
  • Does this answer your question : https://stackoverflow.com/questions/20637439/skip-rows-during-csv-import-pandas – Rishabh Kumar Feb 13 '21 at 11:08
  • attach a reference of your file – Shijith Feb 13 '21 at 11:16
  • @Shijith Following is the link for reference/sample data. https://docs.google.com/spreadsheets/d/1MxJDErUDCd-8ElwvIrE6V6DE78IhXlr1yfh4VNAp9UM/edit#gid=666820361 – srinivas Feb 13 '21 at 12:47
  • @RishabhKumar I have different files with different number of rows to be skipped. I am unsure about how to address it. – srinivas Feb 13 '21 at 12:48

2 Answers2

2

Those lines seem to begin with #, so you can probably use the comment parameter:

comment str, optional

Indicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing #empty\na,b,c\n1,2,3 with header=0 will result in ‘a,b,c’ being treated as the header.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
0

You can use pandas read_csv() function (see documentation here) to read the csv-file. In this function you can add an argument called "skiprows" and define the number of rows that should be skipped when reading the file.

Jonas
  • 1,749
  • 1
  • 10
  • 18