Reputation: 2219
I need to select the row previous to when discharge_location
is Readmit
within a group. Then set the ID and Discharge_location to a dictionary.
sample df
ID admit discharge discharge_location first
20 3-4-2018 3-6-2018 Home 1
20 2-2-2018 2-6-2018 Home 0
20 2-5-2018 2-23-2018 Readmit 0
30 1-2-2018 2-3-2018 Rehab 1
30 1-15-2018 1-18-2018 Readmit 0
30 1-20-2018 1-24-2018 Home 0
40 1-20-2018 1-24-2018 Home 0
40 1-20-2018 1-24-2018 Home 0
40 1-20-2018 1-24-2018 Home 0
final result
ID Discharge_location
20 Home
30 Rehab
Upvotes: 2
Views: 542
Reputation: 323286
IIUC
df.loc[df[df.discharge_location=='Readmit'].index-1]
Out[1519]:
ID admit discharge discharge_location first
1 20 2-2-2018 2-6-2018 Home 0
3 30 1-2-2018 2-3-2018 Rehab 1
From MartyB
df.loc[df[df.discharge_location=='Readmit'].index-1][['ID','discharge_location']].set_index('ID').to_dict()
Upvotes: 1