通过多列熊猫解析

时间:2018-07-09 13:02:55

标签: python pandas dataframe indexing

以下是数据:

raw_data = {'first_name': ['Jason', 'Jason', 'Tina', 'Jake', 'Amy'], 
            'last_name': ['Miller', 'Miller', 'Ali', 'Milner', 'Cooze'], 
            'age': [42, 42, 36, 24, 73], 
            'preTestScore': [4, 4, 31, 2, 3],
            'postTestScore': [25, 25, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])

现在,我想搜索数据帧并查找两个列值,以获取对应的两个列值。

这是我尝试过的-

a,b=df['first_name','last_name'].where(df['age','preTestScore']==42,4)

但出现错误

KeyError                                  Traceback (most recent call last)
E:\anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3063             try:
-> 3064                 return self._engine.get_loc(key)
   3065             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: ('first_name', 'last_name')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-07dc94043416> in <module>()
----> 1 a,b=df['first_name','last_name'].where(df['age','preTestScore']==42,4)

E:\anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

E:\anaconda\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

E:\anaconda\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2484         res = cache.get(item)
   2485         if res is None:
-> 2486             values = self._data.get(item)
   2487             res = self._box_item_values(item, values)
   2488             cache[item] = res

E:\anaconda\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

E:\anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3064                 return self._engine.get_loc(key)
   3065             except KeyError:
-> 3066                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3067 
   3068         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: ('first_name', 'last_name')

2 个答案:

答案 0 :(得分:2)

您可以使用set_index将索引转换为MultiIndex ('age', 'preTestScore')。然后将pd.DataFrame.loc与行和列标签一起使用:

df2 = df.set_index(['age', 'preTestScore'])

cols = ['first_name', 'last_name']
res = df2.loc[(42, 4), cols].values.tolist()

print(res)

[['Jason', 'Miller']]

答案 1 :(得分:0)

我认为需要比较两个值并为每行所有library(sf) x <- st_sfc( st_polygon( list( rbind( c( 39.9912, 116.316 ), c( 39.9912, 116.3153 ), c( 39.9911, 116.3145 ), c( 39.9911, 116.3136 ), c( 39.9911, 116.3127 ), c( 39.9912, 116.3127 ) ) ) ), crs = st_crs(4326), check_ring_dir = FALSE) plot(x, axes = TRUE, graticule = TRUE) 添加all

True

详细信息

m = (df[['age','preTestScore']].values == np.array([42, 4])).all(axis=1)
print (m)
[ True  True False False False]

a = df.loc[m, ['first_name','last_name']].drop_duplicates()
print (a)
  first_name last_name
0      Jason    Miller