[尝试从CSV删除值时出现键错误0

问题描述 投票:0回答:1

我有一个看起来像这样的CSV文件:

Longitude       Latitude    Value
-123.603607     81.377536   0.348
-124.017502     81.387791   0.386
-124.432344     81.397611   0.383
-124.848099     81.406995   0.405
-125.264724     81.415942   --
...            ...         ...

我有一个代码,应该使用勾股定理来删除任何纬度/纬度不在该点的半径范围(-111.55,75.6)的0.7 lon / lat以内的行。当(-111.55-经度)^ 2 +(75.6-纬度)^ 2)>(0.7)^ 2时,if函数应该删除任何行。

import pandas as pd
import numpy
import math
df =pd.read_csv(r"C:\\Users\\tx163s\\Documents\\projectfiles\\values.csv")
drop_indices = []
for row in range(len(df)):
   if ((-111.55-df[row]['Longitude'])**2+(75.6-df[row]['Latitude'])**2) > 0.49:
      drop_indices.append(i)
df.drop(drop_indices, axis=0, inplace=True)
df.to_csv(r"C:\\Users\\tx163s\\Documents\\projectfiles\\values.csv")

但是,我一直收到关键错误0。是因为我试图追加到列表中吗?我该如何解决?

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, 
tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-14-1812652bd9f4> in <module>
      2 
      3 for row in range(len(df)):
----> 4    if ((-71.12167-df[row]['Longitude'])**2+(40.98083-df[row]['Latitude'])**2) > 0.0625:
      5       drop_indices.append(i)
      6 df.drop(drop_indices, axis=0, inplace=True)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
    2925             if self.columns.nlevels > 1:
    2926                 return self._getitem_multilevel(key)
 -> 2927             indexer = self.columns.get_loc(key)
    2928             if is_integer(indexer):
    2929                 indexer = [indexer]

 C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, 
 method, tolerance)
    2657                 return self._engine.get_loc(key)
    2658             except KeyError:
 -> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
    2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
    2661         if indexer.ndim > 1 or indexer.size > 1:

 pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

 pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

 pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

 pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

 KeyError: 0
pandas keyerror
1个回答
1
投票

在您的代码中,将df[row]['Longitude']更改为df.iloc[row]['Longitude'],然后将drop_indices.append(i)更改为drop_indices.append(row)

drop_indices = []
for row in range(len(df)):
   if ((-111.55-df.iloc[row]['Longitude'])**2+(75.6-df.iloc[row]['Latitude'])**2) > 0.49:
      drop_indices.append(row)
df.drop(drop_indices, axis=0, inplace=True)

但是,更好的解决方案是使用pandas操作:

df = df[((df[['Longitude','Latitude']] - [-111.55, 75.6])**2).sum(axis=1) < 0.7**2]
© www.soinside.com 2019 - 2024. All rights reserved.