【问题标题】:Pandas: No column names in data file熊猫:数据文件中没有列名
【发布时间】:2016-11-08 17:55:08
【问题描述】:

我正在尝试处理用于 DataScience 的数据集,但它没有列名。 df.head()的输出如下图:

   1  73                  Not in universe   0  0.1   0.2  Not in universe.1
0  2  58   Self-employed-not incorporated   4   34     0    Not in universe
1  3  18                  Not in universe   0    0     0        High school
2  4   9                  Not in universe   0    0     0    Not in universe
3  5  10                  Not in universe   0    0     0    Not in universe
4  6  48                          Private  40   10  1200    Not in universe

我想看到的是

0  1  73                  Not in universe   0  0.1   0.2  Not in universe.1
1  2  58   Self-employed-not incorporated   4   34     0    Not in universe
2  3  18                  Not in universe   0    0     0        High school
3  4   9                  Not in universe   0    0     0    Not in universe
4  5  10                  Not in universe   0    0     0    Not in universe
5  6  48                          Private  40   10  1200    Not in universe

我可以分配随机的列名,但有更好的方法吗?

【问题讨论】:

    标签: python python-2.7 numpy pandas


    【解决方案1】:

    你加载文件时没有指定它是否有标题行,默认情况下它从第一行推断它,如果它丢失然后传递header=None

    df = pd.read_csv(file_path, header=None)
    

    【讨论】:

      【解决方案2】:

      我希望你通过this link。标头的默认值为“推断”,这意味着如果未指定,它将自动设置数据的整数值。

      您还可以通过设置名称参数来设置不同的列名,该参数采用数组、列名列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        • 2018-11-06
        • 1970-01-01
        • 2020-07-22
        • 2020-02-02
        相关资源
        最近更新 更多