如何摆脱Dataframe中列名顶部的数字?

时间:2016-08-02 13:30:00

标签: pandas dataframe python-3.5

对不起,如果这是基本问题,

我正在将几个csv文件读入数据帧,文件中已有列名。当我读入文件时,程序显示列名称及其对应的数值。我怎么能摆脱这个?

上面列名和数值的数据框示例:

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

我希望它看起来像是什么:

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

代码:

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)

上述代码的结果:

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')

提前谢谢你。

1 个答案:

答案 0 :(得分:2)

请勿设置header=None

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

这会将列名默认为序号位置