首页 > 解决方案 > 传递列名列表以提取熊猫数据框中的数据子集时出错

问题描述

我想在熊猫数据框中选择多列。我正在传递一个列名列表来选择数据子集。然后我得到了一个关键错误。如果我明确输入该特定列名,那就没问题了。你能帮我看看吗?谢谢



data_vaf_eng['Liner_ShaleDepth_Variation_Radius']
0     1.623674
1     0.549020
2     0.022226
3     0.977571
4     1.375821
5     1.575320
6     0.182994
7     1.567821
8     0.265744
9     0.615348
10    1.143208
11    0.000000
12    0.424624
13    0.111889
14    1.840431
15    0.726556
16    0.051500
Name: Liner_ShaleDepth_Variation_Radius, dtype: float64


x_coef
x_coef
['PV_CumSteam_Adjacent7Rows',
 'Liner_STJDepth_Variation_Radius',
 'Liner_ShaleJDepth_Variation_Radius']


data_vaf_eng_x_coef=data_vaf_eng[x_coef]
data_vaf_eng_x_coef.head()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1685-43475f9f9988> in <module>
----> 1 data_vaf_eng_x_coef=data_vaf_eng[x_coef]
      2 data_vaf_eng_x_coef.head()

C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3028             if is_iterator(key):
   3029                 key = list(key)
-> 3030             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   3031 
   3032         # take() does not accept boolean indexers

C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1264             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1265 
-> 1266         self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
   1267         return keyarr, indexer
   1268 

C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1314             if raise_missing:
   1315                 not_found = list(set(key) - set(ax))
-> 1316                 raise KeyError(f"{not_found} not in index")
   1317 
   1318             not_found = key[missing_mask]

KeyError: "['Liner_ShaleJDepth_Variation_Radius'] not in index"


标签: python

解决方案


推荐阅读