gtomer
gtomer

Reputation: 6564

Mapping from a different dataframe

I have a dataset of patients, e.g.:

enter image description here

and a dataset of diseases of each patient (by ICD code):

enter image description here

How can I flag each patient if he had history of a specific ICD code, desired output:

enter image description here

I am currently doing it with iteration but this takes too long....

Upvotes: 1

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 862481

If need indicators - it means only 0, 1 values use get_dummies:

df1 = df1.join(pd.get_dummies(df2.set_index('patient_id')['ICD']).max(level=0), on='patient_id')

If need counts ICD use crosstab:

df2 = df1.join(pd.crosstab(df['patient_id'], df['ICD']), on='patient_id')

Difference is if duplicates in pairs patient_id, ICD.

Upvotes: 2

Related Questions