Python 使用pandas read_csv时仅返回一列
这是我的csv文件Python 使用pandas read_csv时仅返回一列,python,pandas,visual-studio-code,Python,Pandas,Visual Studio Code,这是我的csv文件 Country 2010 2011 2012 2013 2014 Albania 5.5 19.1 6.9 15.4 8 Algeria 18.2 6 8.1 20 9.5 American Samoa 14.1 13.8 3 14.7 2.3 Andorra 16 3
Country 2010 2011 2012 2013 2014
Albania 5.5 19.1 6.9 15.4 8
Algeria 18.2 6 8.1 20 9.5
American Samoa 14.1 13.8 3 14.7 2.3
Andorra 16 3 13.6 12.4 8.3
Angola 17.8 9.8 8.8 6.5 5.5
我的代码:
import pandas as pd
test = pd.read_csv('C:\\Users\\Wing\\Desktop\\TEST.csv',sep='\t')
print(test.head())
print(test.shape)
print(test.columns)
print(test.dtypes)
输出:
the print test.shape command will output (14,1)
the print test.columns command will output(Index(['Country,2010,2011,2012,2013,2014'], dtype='object')
那么我如何将它们分为6列您的csv文件看起来不错:
In [ ]: from io import StringIO
...: import pandas as pd
...: TESTDATA = StringIO("""Country 2010 2011 2012 2013 2014
...: Albania 5.5 19.1 6.9 15.4 8
...: Algeria 18.2 6 8.1 20 9.5
...: American Samoa 14.1 13.8 3 14.7 2.3
...: Andorra 16 3 13.6 12.4 8.3
...: Angola 17.8 9.8 8.8 6.5 5.5""")
...: test = pd.read_csv(TESTDATA,sep='\t')
...: print(test)
Country 2010 2011 2012 2013 2014
0 Albania 5.5 19.1 6.9 15.4 8
1 Algeria 18.2 6 8.1 20 ...
2 American Samoa 14.1 13.8 3 14.7 ...
3 Andorra 16 3 13.6 12.4 ...
4 Angola 17.8 9.8 8.8 6.5 ...
它对我来说工作得非常好,但是如果我使用print(test.shape)它显示(6,1)它意味着6行1列,因此我不能使用print(test['2010'])命令,它将输出keyrerror:'2010',似乎列名变为(Country 2010 2011 2013 2014)而不是指定为6列(Country,2010,2011…等等)