0

pandas DataFrame を反復処理し、特定の列に連続した重複がある行を削除する関数があります。その後、その列の実行中の合計をリストに返そうとしましたが、キーエラーが発生しているようです。これが何を意味するのかわかりません。

最小限のコード:

dropRows = [] #stores rows indices to drop
#Sanitize the data to get rid of consecutive duplicates
for indx, val in enumerate(df.removeConsecutives): #for all the values
    if(indx == 0): #skip first indx
        continue

    if (val == df.removeConsecutives[indx-1]): #this is duplicate value as the last one
        dropRows.append(indx)

sanitizedData = df.drop(dropRows)

#Create Timestamps based on RTC
listOfSums= [0] #first sum is zero
sum = 0 #running total of seconds for timestamps
for indx, val in enumerate(sanitizedData.removeConsecutives):

    sum += sanitizedData.removeConsecutives[indx]

    listOfSums.append(sum) #add running sum to list

エラートレースはこの行を指しています

    listOfSums.append(sum) #add running sum to list

そして、これはエラーです

C:\Users\JohnDoe\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_value (pandas\index.c:2987)()

C:\Users\JohnDoe\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_value (pandas\index.c:2802)()

C:\Users\JohnDoe\Anaconda\lib\site-packages\pandas\index.pyd in pandas.index.IndexEngine.get_loc (pandas\index.c:3528)()

C:\Users\JohnDoe\Anaconda\lib\site-packages\pandas\hashtable.pyd in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7032)()

C:\Users\JohnDoe\Anaconda\lib\site-packages\pandas\hashtable.pyd in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6973)()

KeyError: 150L

すべてのパッケージ (pandas、numpy、SciPy など) を 1 つのインストール ファイルにインストールする iPython を使用しているため、パスに anaconda が含まれているのはそのためです。

4

1 に答える 1

2

ここ:

for indx, val in enumerate(sanitizedData .band_rtc):
    sum += sanitizedData.removeConsecutives[indx]

enumerate を使用しています。つまり、indx変数は 0 から sanitizedData の行数になります。ただし、removeConsecutivesシリーズ連番で索引付けされていません。おそらく以前は - しかし、使用した後ではありませんdrop

例 - 300 行の df があったとします。行 150 で重複を見つけて削除しました。これで、df には 0 ~ 149、151 ~ 299 のインデックスが付けられた 299 行が含まれます。しかしindx、0 から 298 になり、150 にアクセスしようとします! これは、次を使用する場合におそらく機能します。

for indx, val in enumerate(sanitizedData .band_rtc):
    sum += sanitizedData.removeConsecutives.iloc[indx]

これはあなたの質問に関するものですが、drop_duplicatessumをご覧になることをお勧めします。

于 2014-09-08T05:57:44.393 に答える