0

I have a csv file with a wrong first row data. The names of labels are in the row number 2. So when I am storing this file to the DataFrame the names of labels are incorrect. And correct names become values of the row 0. Is there any function similar to reset_index() but for columns? PS I can not change csv file. Here is an image for better understanding. DataFrame with wrong labels

Alona
  • 67
  • 1
  • 10
  • Does this answer your question? https://stackoverflow.com/questions/20637439/skip-rows-during-csv-import-pandas – IoaTzimas Oct 06 '20 at 14:33

2 Answers2

0

Hello let's suppose you csv file is data.csv :

Try this code:

import pandas as pd

#reading the csv file
df = pd.read_csv('data.csv')

#changing the headers name to integers 
df.columns = range(df.shape[1])

#saving the data in another csv file
df.to_csv('data_without_header.csv',header=None,index=False)

#reading the new csv file
new_df = pd.read_csv('data_without_header.csv')

#plotting the new data
new_df.head()
  • thank you! This also can be a solution! But I prefer previous answer because it is having much less code :P – Alona Oct 06 '20 at 15:05
-1

If you do not care about the rows preceding your column names, you can pass in the "header" argument with the value of the correct row, for example if the proper column names are in row 2:

df = pd.read_csv('my_csv.csv', header=2)

Keep in mind that this will erase the previous rows from the DataFrame. If you still want to keep them, you can do the following thing:

df = pd.read_csv('my_csv.csv')
df.columns = df.iloc[2, :] # replace columns with values in row 2

Cheers.

sumnuz
  • 45
  • 7