尝试获取所有非';t在python 3中等于0.000000
我有一个如下所示的数据集,我试图抓住feature列中重要性列不等于0.000000的每个名称,并将它们直接放入一个列表中立即使用。我尝试过几种方法,但最有希望的两种方法如下: 方法1 方法1只是获取重要性列的所有值,但是我想要特征列的值,所以我尝试了方法2 方法2 方法2向我抛出如下错误: 方法2错误 用于创建数据帧的行 数据帧图片 新测试尝试获取所有非';t在python 3中等于0.000000,python,python-3.x,pandas,dataframe,anaconda,Python,Python 3.x,Pandas,Dataframe,Anaconda,我有一个如下所示的数据集,我试图抓住feature列中重要性列不等于0.000000的每个名称,并将它们直接放入一个列表中立即使用。我尝试过几种方法,但最有希望的两种方法如下: 方法1 方法1只是获取重要性列的所有值,但是我想要特征列的值,所以我尝试了方法2 方法2 方法2向我抛出如下错误: 方法2错误 用于创建数据帧的行 数据帧图片 新测试 #要使用的功能=[] a、 b=重要性排名[0] #对于重要性排名中的s、x、y: #如果y>0.000000: #功能\u到\u使用。附加(x) #
#要使用的功能=[]
a、 b=重要性排名[0]
#对于重要性排名中的s、x、y:
#如果y>0.000000:
#功能\u到\u使用。附加(x)
#
#要使用的功能
KeyError回溯(最近一次呼叫最后一次)
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2524请尝试:
->2525返回发动机。获取位置(钥匙)
2526除键错误外:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
关键错误:0
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
1#功能_to_use=[]
---->2 a,b=重要性排名[0]
3#对于重要性排序中的s、x、y:
4#如果y>0.000000:
5#功能_to_use.追加(x)
~\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuuu(self,key)
2137返回自我。\u获取项目\u多级(键)
2138其他:
->2139返回self.\u getitem\u列(键)
2140
2141 def_getitem_列(自身,键):
~\Anaconda3\lib\site packages\pandas\core\frame.py在\u getitem\u列中(self,key)
2144#获取列
2145如果self.columns.u是唯一的:
->2146返回自我。获取项目缓存(密钥)
2147
2148#重复列和可能的降维
~\Anaconda3\lib\site packages\pandas\core\generic.py在\u get\u item\u缓存中(self,item)
1840 res=cache.get(项)
1841如果res为无:
->1842 values=self.\u data.get(项目)
1843 res=自身。\框\项\值(项,值)
1844缓存[项目]=res
get中的~\Anaconda3\lib\site packages\pandas\core\internals.py(self、item、fastpath)
3841
3842如果不是isna(项目):
->3843 loc=自身项目。获取loc(项目)
3844其他:
3845索引器=np.arange(len(self.items))[isna(self.items)]
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2525返回发动机。获取位置(钥匙)
2526除键错误外:
->2527返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2528
2529 indexer=self.get_indexer([key],method=method,tolerance=tolerance)
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
关键错误:0
我认为最好的办法是使用:
然后获取以下功能:
features = df.features
我认为最好的办法是使用: 然后获取以下功能:
features = df.features
数据帧提供了一种选择所需数据的好方法
features_to_use = importance_ranking[importance_ranking['importance'] > 0.0]['importance'].values.tolist()
乍一看可能很难理解,但实际上您要做的是过滤所有重要性大于0.0的重要性排名,然后选择满足此条件的重要性排名的重要性列。行的其余部分.values.tolist()
仅用于解压缩数据
如果您对此解决方案感到不舒服,您可以尝试一步一步地进行:
df = importance_ranking[importance_ranking['importance'] > 0.0] # Filtered Dataframe
importance_values = df['importance'] # Series Object
features_to_use = importance_values.values.tolist()
数据帧提供了一种选择所需数据的好方法
features_to_use = importance_ranking[importance_ranking['importance'] > 0.0]['importance'].values.tolist()
乍一看可能很难理解,但实际上您要做的是过滤所有重要性大于0.0的重要性排名,然后选择满足此条件的重要性排名的重要性列。行的其余部分.values.tolist()
仅用于解压缩数据
如果您对此解决方案感到不舒服,您可以尝试一步一步地进行:
df = importance_ranking[importance_ranking['importance'] > 0.0] # Filtered Dataframe
importance_values = df['importance'] # Series Object
features_to_use = importance_values.values.tolist()
带数字的第一列是文件的一部分吗?看起来每行中有3个值,因此将其拆分为两个会引发错误。@kabanus的可能重复项是的,我认为是的,我尝试在for语句中使用方法2和3个值,但仍然不起作用,然后发布该尝试和错误。特别是,您只需要最后两列,因此类似于
\uuu,a,b
的内容看起来会起作用。@kabanus我添加了新的尝试,作为方法3,第一列带有数字是文件的一部分吗?看起来每行中有3个值,因此将其拆分为两个会引发错误。@kabanus的可能重复项是的,我认为是的,我尝试在for语句中使用方法2和3个值,但仍然不起作用,然后发布该尝试和错误。特别是,您只需要最后两列,因此类似于\uu,a,b
的内容看起来可以工作。@kabanus我添加了新的尝试作为方法3
#features_to_use = []
a,b = importance_ranking[0]
#for s,x,y in importance_ranking:
# if y > 0.000000:
# features_to_use.append(x)
#
#features_to_use
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2524 try:
-> 2525 return self._engine.get_loc(key)
2526 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-1244-5d9e2e614219> in <module>()
1 #features_to_use = []
----> 2 a,b = importance_ranking[0]
3 #for s,x,y in importance_ranking:
4 # if y > 0.000000:
5 # features_to_use.append(x)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
-> 2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
-> 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionality
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1840 res = cache.get(item)
1841 if res is None:
-> 1842 values = self._data.get(item)
1843 res = self._box_item_values(item, values)
1844 cache[item] = res
~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3841
3842 if not isna(item):
-> 3843 loc = self.items.get_loc(item)
3844 else:
3845 indexer = np.arange(len(self.items))[isna(self.items)]
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2525 return self._engine.get_loc(key)
2526 except KeyError:
-> 2527 return self._engine.get_loc(self._maybe_cast_indexer(key))
2528
2529 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
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 = importance_ranking[importance_ranking['importance']>0.000000]
features = df.features
features_to_use = importance_ranking[importance_ranking['importance'] > 0.0]['importance'].values.tolist()
df = importance_ranking[importance_ranking['importance'] > 0.0] # Filtered Dataframe
importance_values = df['importance'] # Series Object
features_to_use = importance_values.values.tolist()