Reputation: 2213
I have created a DataFrame with multi index named key_mat.It just likes
cnt
keys minutes
0000 21:01:00 1
0002 19:08:00 2
16:07:00 1
I want to select a subset of key_mat using .loc
>>> key_mat.loc[['0002']]
>>>
cnt
keys minutes
0002 19:08:00 2
16:07:00 1
Now, I have a list of keys
key_list = ['0000','0001','0004'...]
and a list of minutes
minutes_list = ['19:08:00','20:07:00',...]
.I want to select cnt
column where keys
in key_list
and minutes
in minutes_list
. I try to use .loc
, but I just find the way key_mat.loc[[('0000','21:01:00'),('0002','19:08:00')]]['cnt']
.But this can't satisfy my needs. I find a way to use query
,key_mat.query('keys in {key_list} and minutes in {minutes_list}'.format(key_list = key_list, minutes_list =minutes_list))
.Is there a way to use .loc
or .ix
to finish my task?
Upvotes: 1
Views: 628
Reputation: 862641
You need slicers, but first sort_index
, because error:
KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (1)'
key_list = ['0000','0001','0004']
minutes_list = ['19:08:00','21:01:00']
df.sort_index(inplace=True)
idx = pd.IndexSlice
print (df.loc[idx[key_list,minutes_list],:])
cnt
keys minutes
0000 21:01:00 1
Upvotes: 1