IndexError阻碍代码处理较大的csv文件

时间:2015-06-19 20:52:15

标签: python csv indexing pandas

我有数据通过使用groupby对csv进行排序,然后绘制信息。我使用了一小部分信息来创建代码。它运行顺利,然后我尝试用巨大的数据文件运行它。

我是Python的新手,这个问题非常令人沮丧,所以即使是如何解决这个问题的建议也会有所帮助。

我的代码在此部分停止:

import pandas as pd

df =pd.DataFrame.from_csv('MYDATA.csv')
mode = lambda ts: ts.value_counts(sort=True).index[0]

我尝试只选择部分巨大的数据文件然后运行,但是对于整个事情我收到了这个错误:

IndexError:索引0超出轴0,大小为0

但我已经并排查看了两个数据集并且列是相同的!我注意到大文件在重音方面有一些utf8问题,我正在努力梳理这些问题,但是这个IndexError令我感到困惑。

这是追溯

runfile('C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests/TopSixCustomersExecute.py', wdir='C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests')
Traceback (most recent call last):

  File "<ipython-input-45-53a2a006076e>", line 1, in <module>
    runfile('C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests/TopSixCustomersExecute.py', wdir='C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests')

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests/TopSixCustomersExecute.py", line 23, in <module>
    df = df.groupby('CompanyName')[['Column1','Name', 'Birthday', 'Country', 'County']].agg(mode).T.reindex(columns=cols)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 676, in agg
    return self.aggregate(func, *args, **kwargs)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2674, in aggregate
    result = self._aggregate_generic(arg, *args, **kwargs)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2722, in _aggregate_generic
    return self._aggregate_item_by_item(func, *args, **kwargs)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2751, in _aggregate_item_by_item
    colg.aggregate(func, *args, **kwargs), data)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2307, in aggregate
    result = self._aggregate_named(func_or_funcs, *args, **kwargs)

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2394, in _aggregate_named
    output = func(group, *args, **kwargs)

  File "C:/Users/jbyrusb/Documents/Python Scripts/Tests/tests/TopSixCustomersExecute.py", line 20, in <lambda>
    mode = lambda ts: ts.value_counts(sort=True).index[0]

  File "C:\Users\jbyrusb\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\index.py", line 915, in __getitem__
    return getitem(key)

IndexError: index 0 is out of bounds for axis 0 with size 0

2 个答案:

答案 0 :(得分:1)

如果没有看到导致错误的数据很困难,请尝试这样做:

mode = (lambda ts: ts.value_counts(sort=True).index[0] 
                   if len(ts.value_counts(sort=True)) else None)

答案 1 :(得分:0)

我通过更改sep参数解决了同样的问题 九月= '\ T'  至     九月= ''

希望能救人。