ubuntu_noob
ubuntu_noob

Reputation: 2365

Parsing through multiple columns pandas

Here is the data:

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'])

Now I want to search through the dataframes and for two column values I want to get the corresponding two column values.

Here is what I tried-

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

but its getting the error

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')

Upvotes: 1

Views: 1100

Answers (2)

jezrael
jezrael

Reputation: 862591

I think need compare both values and add all for all Trues per rows:

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

Detail:

print ((df[['age','preTestScore']].values == np.array([42, 4])))

[[ True  True]
 [ True  True]
 [False False]
 [False False]
 [False False]]

Upvotes: 0

jpp
jpp

Reputation: 164643

You can use set_index to convert your index to a MultiIndex ('age', 'preTestScore'). Then use pd.DataFrame.loc with row and column labels:

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

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

print(res)

[['Jason', 'Miller']]

Upvotes: 2

Related Questions