【问题标题】:How to get rid of the numbers at the top of column names in Dataframe?如何摆脱Dataframe中列名顶部的数字?
【发布时间】:2016-12-07 21:25:35
【问题描述】:

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

我正在将几个 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')

提前谢谢你。

【问题讨论】:

  • 你能发布你的csv的前几行

标签: pandas dataframe python-3.5


【解决方案1】:

不要设置header=None:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多