使用pandas read_cs时只返回一列

2024-04-26 05:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的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       13.6    12.4    8.3
Angola          17.8    9.8     8.8     6.5     5.5

我的代码:

^{pr2}$

输出:

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列呢


Tags: 文件csvthetestoutputcountrywillcommand
1条回答
网友
1楼 · 发布于 2024-04-26 05:25:02

你的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   ...

相关问题 更多 >