RustyShackleford
RustyShackleford

Reputation: 3667

How to get rid of the numbers at the top of column names in Dataframe?

sorry if this is basic questions,

I am reading few csv files into a dataframe, the files have column names already in the file. When I read in the file the program shows the column names plus their corresponding numerical value. How can I get rid of this?

Example of dataframe with column name and numerical value above:

 29          30          31  
0       PHONE_NUM         LMA         IMA  
1             NaN  UNASSIGNED  UNASSIGNED  
2             NaN  UNASSIGNED  UNASSIGNED  
3             NaN  UNASSIGNED  UNASSIGNED

What I want it to look like:

        PHONE_NUM         LMA         IMA  
0             NaN  UNASSIGNED  UNASSIGNED  
1             NaN  UNASSIGNED  UNASSIGNED  
2             NaN  UNASSIGNED  UNASSIGNED

Code:

import pandas as pd

path = 'File_Path'

account_info = pd.read_csv(path + '/file.csv' ,header = None,
                           encoding = 'iso-8859-1',error_bad_lines = False)

print(account_info.columns)

Result of above code:

Int64Index([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
           dtype='int64')

Thank you in advance.

Upvotes: 1

Views: 112

Answers (1)

EdChum
EdChum

Reputation: 394031

Don't set header=None:

account_info = pd.read_csv(path + '/file.csv' ,
                           encoding = 'iso-8859-1',error_bad_lines = False)

This will default the column names to the ordinal position

Upvotes: 2

Related Questions