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

Key Error 0 when trying to delete values from CSV

我有一个如下所示的 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   --
...            ...         ...

我有一个代码应该使用毕达哥拉斯定理删除 latitude/latitude 不在点 (-111.55,75.6) 半径 0.7 lon/lat 范围内的任何行。当 (-111.55-Longitude)^2+(75.6-Latitude)^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

在您的代码中,将 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]