0

I am having a pandas data frame as follows:

df1=pd.DataFrame({'customerId': ['1', '2','1','1','3'], 'productId': ['111','112','113','114','111'],'index':[1.0,3.0,4.0,2.5,6.3]})
df1[['customerId','productId','index']]

  customerId    productId   index
0     1          111         1.0
1     2          112         3.0
2     1          113         4.0
3     1          114         2.5
4     3          111         6.3

And I want to create a pandas data frame where values in the productId column will become column names and the values under these columns are the corrosponding values in index column for that customerId as follows:

  customerId    111    112   113    114
0    1          1.0    0.0   4.0    2.5
1    2          0.0    3.0   0.0    0.0
2    3          6.3    0.0   0.0    0.0

NOTE: since this can be done using while/for loop but I want to perform it in Pandas operation only since the data frame I am working is huge and loops will take a lot of time. Hence please suggest any efficient way to do this in Pandas.

Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19

0 Answers0