【问题标题】:Key Error 0 when trying to delete values from CSV尝试从 CSV 中删除值时出现键错误 0
【发布时间】:2019-12-20 17:15:26
【问题描述】:

我有一个如下所示的 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 范围内的任何行。 if 函数应该在 (-111.55-Longitude)^2+(75.6-Latitude)^2)>(0.7)^2 时删除任何行。

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] 对列进行切片,并且您没有标记为 0 的列。你会想要df.iloc[row],尽管循环可能完全没有必要。

标签: pandas keyerror


【解决方案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]

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 1970-01-01
    • 2014-12-02
    • 2015-12-08
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多