read_csv不会打开csv

时间:2015-07-22 22:05:43

标签: csv pandas

我尝试使用pandas模块打开csv文件(http://archive.ics.uci.edu/ml/machine-learning-databases/00222/),但是read_csv命令没有正确打开文件。

import pandas
bankfull = pandas.read_csv('bank-full.csv')
print bankfull.head()

,结果看起来像

age;"job";"marital";"education";"default";"balance";"housing";"loan";"contact";"day";"month";"duration";"campaign";"pdays";"previous";"poutcome";"y
0  58;"management";"married";"tertiary";"no";2143...                                                                                                  

1 44;“技师”;“单身”;“中学”;“不”; 29;“......

如何修复代码以便将csv文件导入为适当的pandas Dataframe? 谢谢!

1 个答案:

答案 0 :(得分:2)

您需要设置分隔符arg sep=';',默认为逗号,,您可以查看read_csv的文档:

pd.read_csv('bank-full.csv', sep=';')
Out[27]:
       age            job   marital  education default  balance housing loan  \
0       58     management   married   tertiary      no     2143     yes   no   
1       44     technician    single  secondary      no       29     yes   no   
2       33   entrepreneur   married  secondary      no        2     yes  yes   
3       47    blue-collar   married    unknown      no     1506     yes   no   
4       33        unknown    single    unknown      no        1      no   no   
5       35     management   married   tertiary      no      231     yes   no   
6       28     management    single   tertiary      no      447     yes  yes   
7       42   entrepreneur  divorced   tertiary     yes        2     yes   no   
8       58        retired   married    primary      no      121     yes   no   
9       43     technician    single  secondary      no      593     yes   no   
10      41         admin.  divorced  secondary      no      270     yes   no   
11      29         admin.    single  secondary      no      390     yes   no