I'm looking to create category-specific columns based the corresponding category for some of the columns.
I've accomplished this in a round-about way by (1) slicing the 2 categories into two separate dataframes, (2) merging the two dataframes on the date (3) deleting redundant columns (4) creating new columns (category agnostic) (4) delete category specific columns. Do you know of a more efficient way to do this transfomration? My code is below the example input/output
Input:
wk start car rims color Autopilot$ Sunroof$
0 2018-09-09 tesla model x 17 black 3000 0
1 2018-09-16 tesla model x 14 yellow 3000 0
2 2018-09-23 tesla model x 13 white 3000 0
3 2018-09-09 tesla model 3 19 grey 0 2000
4 2018-09-16 tesla model 3 21 pink 0 2000
Ideal Output:
wk rims-mod3 rims-modx color-mod3 color-modx Auto$ roof$
0 2018-09-09 17 0 black grey 3000 2000
1 2018-09-16 14 19 yellow pink 3000 2000
2 2018-09-23 13 21 white NaN 3000 0
My code:
import pandas as pd
df = pd.DataFrame({'wk start': ['2018-09-09', '2018-09-16', '2018-09-23','2018-09-09', '2018-09-16'],
'car': [ 'tesla model x', 'tesla model x', 'tesla model x','tesla model 3','tesla model 3'],
'rims': [17,14,13,19,21],
'color':['black','yellow','white','grey','pink'],
'Autopilot$':[3000,3000, 3000,0,0],
'Sunroof$':[0,0,0,2000,2000]})
model3 = df[df['car']=='tesla model 3']
modelx = df[df['car']=='tesla model x']
example = model3.merge(modelx, how='outer',left_on='wk start',right_on='wk start',suffixes=('_model3', '_modelx'))
del example['car_model3']
del example['car_modelx']
example['AUTOPILOT']=example['Autopilot$_model3']+example['Autopilot$_modelx']
example['SUNROOF']=example['Sunroof$_model3']+example['Sunroof$_modelx']
del example['Autopilot$_model3']
del example['Autopilot$_modelx']
del example['Sunroof$_modelx']
del example['Sunroof$_model3']