从熊猫数据框中的列中删除特定的值

时间:2019-08-07 18:35:17

标签: python pandas numpy

我需要删除在熊猫数据框中包含特定列值的特定行。

import numpy as np
import pandas as pd
import copy


### Choose the columns we will use
usecols = [str(x) for x in ["dered_r","u_g_color","g_r_color","r_i_color","i_z_color","diff_u",\
           "diff_g1","diff_i","diff_z","class"]]

### Load the data 
dataset = pd.read_csv('STAR_data.csv',index_col=0, usecols=usecols)
dataset = dataset.append(pd.read_csv("GALAXY_data.csv",index_col=0, usecols=usecols))
dataset = dataset.append(pd.read_csv("QSO_data.csv",index_col=0, usecols=usecols))

### Fix the data
dataset = dataset[dataset["dered_r"] > -9999] 
dataset = dataset[(dataset["g_r_color"] > -10)]
dataset = dataset[(dataset["g_r_color"] < 10)]

我想删除dered_r列下任何值小于-9999的行。对于g_r_color我只想要介于10和-10之间的行。我写的代码给出了错误:

Traceback (most recent call last):
 File "C:\Users\ABRA\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2890, in get_loc
   return self._engine.get_loc(key)
 File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
 File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
 File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
 File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'dered_r'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "data_extractor.py", line 19, in <module>
   dataset = dataset[dataset["dered_r"] > -9999]
 File "C:\Users\ABRA\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2975, in __getitem__
   indexer = self.columns.get_loc(key)
 File "C:\Users\ABRA\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2892, in get_loc
   return self._engine.get_loc(self._maybe_cast_indexer(key))
 File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
 File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
 File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
 File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'dered_r'

0 个答案:

没有答案