I'm working with pandas dataframe. I have datafreme like:
df
COUNTRY LINE PRODUCT SERVICE
Argelia 1 1.0 Mobile
Argelia 1 2.0 Mobile
Argelia 1 3.0 Mobile
Argelia 2 1.0 Mobile
Argelia 3 2.0 Mobile
Argelia 3 3.0 Mobile
I want to group by LINE and pivot PRODUCT column, but i need 4 product columns (product_1, product_2, product_3 and product_4), it don't care if there are any PRODUCT value = 4 or not.
I'm trying to use get_dummies
with this code:
df = pd.concat([df, pd.get_dummies(dfs['PRODUCT'], prefix='product')], axis=1)
df.drop(['PRODUCT'], axis=1, inplace=True)
df = df.groupby(['COUNTRY', 'LINE', 'SERVICE']).agg({'product_1' : np.max, 'product_2': np.max, 'product_3':np.max, 'product_4':np.max}).reset_index()
But it give me only 3 columns of product, I want 4 columns to have this dataframe:
COUNTRY LINE SERVICE product_1 product_2 product_3 product_4
Argelia 1 Mobile 1 1 1 0
Argelia 2 Mobile 1 0 0 0
Argelia 3 Mobile 0 1 1 0
Is it possible?
(I need to change PRODUCT values type 1.0 to 1 too)