简体   繁体   English

使用 pandas 从 csv 文件中读取特征名称

[英]Reading feature names from a csv file using pandas

I have a csv file looks like我有一个 csv 文件看起来像

        F1    F2    F3
 A1      2     4     2
 A2      4     1     2

When I read the file using pandas, I see that the first column is unnamed.当我使用 pandas 读取文件时,我看到第一列未命名。

import pandas as pd
df = pd.read_csv("data.csv")
features = df.columns
print( features )

Index(['Unnamed: 0', 'F1, 'F2, 'F3'])

In fact I want to get only F1, F2 and F3.事实上,我只想获得 F1、F2 和 F3。 I can fix that with some array manipulation.我可以通过一些数组操作来解决这个问题。 But I want to know if pandas has some builtin capabilities to do that.但我想知道 pandas 是否有一些内置功能可以做到这一点。 Any thought?任何想法?

UPDATE:更新:

Using index_col = False or None doesn't work either.使用index_col = FalseNone也不起作用。

在此处输入图像描述

That unnamed is only because of index column being read, you can use the index_col = [0] argument in the read statement to resolve.那个unnamed只是因为索引列被读取,可以在读取语句中使用index_col = [0]参数来解决。

This picks the first column as index instead of a feature itself.这选择第一列作为索引而不是特征本身。

import pandas as pd
df = pd.read_csv("data.csv", index_col=[0])
features = df.columns
print( features )

Index(['F1', 'F2', 'F3'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM