未命名的列和熊猫南

时间:2018-09-10 10:07:29

标签: python pandas

当我尝试打印.csv文件的标题时,输出中出现Unnamed and Nan。

将熊猫作为pd导入

df = pd.read_csv('testextract.csv', error_bad_lines=False,sep=' ',dtype=unicode,index_col=0,low_memory=False)
print(df.head())

输出:

 Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6  \
��T                                                                     
NaN        NaN        NaN        NaN        NaN        NaN        NaN   
NaN        NaN        NaN        NaN        NaN        NaN        NaN

3 个答案:

答案 0 :(得分:0)

sick = c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,5,5,7,7,7,7,7,8,8,9)
ft = data.frame(table(sick))
colnames(ft) = c('Value','Freq')
ft$Perc = round(ft$Freq / sum(ft$Freq) * 100,1)
counts = table(factor(sick,levels = c(0:9)))
bp = barplot(counts, main="Sick Leave Distribution", ylab = "Frequency", xlab = "Days of Sick Leave Taken" , ylim = c(0,10), cex.names = 1, col = "blue", names.arg=gsub("\\s","\n", c(0:9)))
text(x=bp, y=counts, labels=round(counts,0), pos=3, xpd=NA)

答案 1 :(得分:0)

您正在读取一个csv文件,并将分隔符用作''。 使用下面的代码 pd.read_csv(文件名,编码='UTF-8')

答案 2 :(得分:0)

我遇到了相同的错误。您必须更改编码以确保它是UTF-8。您可以通过两种方式执行此操作:

  1. 使用熊猫的编码方法,例如:
df = pd.read_csv('testextract.csv', encoding, errors='strict')
  1. 在电子表格应用程序中打开CSV文件,然后另存为UTF-8。然后,再次运行您的代码。

希望这会有所帮助。