I have this dataframe:
df = pd.DataFrame({
'thread_id': [0,0,1,1,1,2,2],
'message_id_in_thread': [0,1,0,1,2,0,1],
'text': ['txt0', 'txt1', 'txt2', 'txt3', 'txt4', 'txt5', 'txt6']
}).set_index(['thread_id', 'message_id_in_thread'])
And I want to keep all the last second level rows, meaning that:
- For
thread_id==0I want to keep the rowmessage_id_in_thread==1 - For
thread_id==1I want to keep the rowmessage_id_in_thread==2 - For
thread_id==2I want to keep the rowmessage_id_in_thread==1
This can easily be achieved by doing df.iterrows(), but I would like to know if there is any direct indexing method.
I look for something like df.loc[(:, -1)], which selects from all (:) level 1 groups, the last (-1) row of that block/group, but obviously this does not work.
