使用熊猫读取csv数据中的某些列

时间:2019-07-19 11:21:13

标签: python pandas csv

我有数据csv

word,centroid
she,1
great,0
good,3
mother,2
father,2
After,4
before,4
.....

我只想看centroid

我的代码是:

df = pd.read_csv('data/hasil_cluster.csv',encoding = "ISO-8859-1") 
print(df['centroid'])

和错误:

KeyError: 'centroid'

1 个答案:

答案 0 :(得分:0)

;之后有centroid

print (df.columns.tolist())
['word', 'centroid;'] 

您可以删除列名之前和之后的所有;

df.columns = df.columns.str.strip(';')

或使用rename

df = df.rename(columns={'centroid;':'centroid'})

或重新分配列名称:

df.columns = ['word', 'centroid']