在控制台中处理print()时出错

时间:2015-02-26 07:30:37

标签: python python-3.x pandas

以下是使用Pandas的python 3上的一些代码,解释器是3.4.1

import pandas as pd

pd.set_option('display.width', 300)
file = open('C:\PythonData\Result.csv', 'w')
file.write('"StopFirstIndex"' + ';' + '"StopLastIndex"' + ';' \
    + '"GOV_NUMBER"' + ';' + '"StopTime"' + '\n')
df1 = pd.read_csv('C:\PythonData\data4.csv', ",", header=0).sort_index(by=['CAR_ID', 'DATE_TO'])
df1.index = range(0, len(df1))
StopFirstIndex = 0
StopLastIndex = 0
CntZeroSpeedRows = 0
for index, row in df1.iterrows():
    if row['SPEED'] == df1.get_value(index + 1, 'SPEED') == 0 and row['CAR_ID'] == df1.get_value(index + 1, 'CAR_ID'):
        if CntZeroSpeedRows >= 1:
            CntZeroSpeedRows += 1
        else:
            CntZeroSpeedRows = 1
    else:
        if CntZeroSpeedRows >= 3:
            StopFirstIndex = index - CntZeroSpeedRows
            StopLastIndex = index
            StopTime = pd.to_datetime(df1.get_value(StopLastIndex, 'DATE_TO'), dayfirst=True) - \
                       pd.to_datetime(df1.get_value(StopFirstIndex, 'DATE_TO'), dayfirst=True)
            tempdf1 = pd.concat([df1[StopFirstIndex:StopLastIndex+1]['LATITUDE'],
                                    df1[StopFirstIndex:StopLastIndex+1]['LONGITUDE']], axis=1)
            file.write('"' + str(StopFirstIndex) + '";"' + str(StopLastIndex) + '";"' \
                     + str(df1.get_value(StopFirstIndex, 'GOV_NUMBER')) + '";"' + str(StopTime) + '"' + '\n')  # making csv
            print(StopFirstIndex, StopLastIndex, df1.get_value(StopFirstIndex, 'GOV_NUMBER'), StopTime)  # printing results in console
        CntZeroSpeedRows = 0
file.close()

制作csv文件没问题,但是如果我在控制台中打印结果,打印完正确的结果后,会显示以下错误:

> Traceback (most recent call last):    
> File "C:/Users/Moiseev/PycharmProjects/untitled1/test1.py", line 21, in <module>
>    if row['SPEED'] == df1.get_value(index + 1, 'SPEED') == 0 and row['CAR_ID'] == df1.get_value(index + 1, 'CAR_ID'):    
> File "C:\Users\Moiseev\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1542, in get_value
return engine.get_value(series.values, index)    
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas\index.c:2993)   
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas\index.c:2808)    
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas\index.c:3534)    
File "hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7035)    
File "hashtable.pyx", line 387, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6976) KeyError: 1000

> Process finished with exit code 1

所以,有两个问题:
    1.这很重要吗?     2.如何删除此错误?

0 个答案:

没有答案