joining 2 panda dataframes with multiple conditions 1 condition is based on a static string

looks like pivot is what you are after :

df.pivot("Model", "Type", "Amount")

Type    Discount    Price   Tax
Model           
Model 1 500.0   10000.0     2000.0
Model 2 NaN     20000.0     4000.0
Model 3 NaN     50000.0     NaN

If you need it to be exactly as your data output above, a few more steps are required :

(df
 .pivot("Model", "Type", "Amount")
 .reindex(df.Type.unique(), axis=1)
 .rename_axis(columns=None)
 .reset_index())

   Model    Price   Tax     Discount
0   Model 1 10000.0 2000.0  500.0
1   Model 2 20000.0 4000.0  NaN
2   Model 3 50000.0 NaN     NaN

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top