使用pandas read_csv时只返回一列

时间:2017-12-05 04:25:34

标签: python pandas visual-studio-code

这是我的csv文件

g++ -m32 ...

我的代码:

Traceback (most recent call last):
  File "timegradient.py", line 3, in <module>
    import matplotlib.pyplot as plt


  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 113, in <module>

    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/__init__.py", line 60, in pylab_setup
    [backend_name], 0)

  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk

  File "/usr/local/lib/python3.5/dist-packages/six.py", line 92, in __get__
    result = self._resolve()

  File "/usr/local/lib/python3.5/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)

  File "/usr/local/lib/python3.5/dist-packages/six.py", line 82, in _import_module
    __import__(name)

  File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')

ImportError: No module named '_tkinter', please install the python3-tk package

输出:

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

所以如何将它们分成6列

1 个答案:

答案 0 :(得分:1)

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