1

I have a data frame that looks like this. I have created 3 new columns that will take the value from other columns.I want function columns separate column and grab total hours for each user for each function.

 User         Function          Total hours   Damage Processing problem solve damages sweeper
 schae      Damage Processing      9.36
 Julie      Problem solve          9.70
 John       sweeper                18.9
 Dan       Damages                 1.83
 Dan       Damages                 1.83
 Julie     Damages                 1.83
 Dan       Problem solve           1.83

The expected output will look like

 User         Function          Total hours   Damage Processing  problem solve damages  sweeper
 schae     Damage Processing       9.36          9.36
 Julie     Problem solve           9.70                            9.70
 John      sweeper                 18.9                                                  18.9
 Dan       Damages                 1.83                                        1.83                                 
 Dan       sweeper                 1.83                                                  1.83
 Julie     Damages                 1.83                                        1.83
 Dan       Problem solve           1.83                            1.83

I think of pd.melt but it throws an error value var not present

res = pd.melt(result,id_vars = ['Function'],value_vars=['Total hours'])
Learner
  • 335
  • 3
  • 16

2 Answers2

2

here is a way using get_dummies and df.assign:

out = (df[['User','Function','Total hours']].assign(**pd.get_dummies(df['Function'])
       .mul(df['Total hours'],axis=0).replace(0,np.nan)))
print(out)

    User           Function  Total hours  Damage Processing  Damages  \
0  schae  Damage Processing         9.36               9.36      NaN   
1  Julie      Problem solve         9.70                NaN      NaN   
2   John            sweeper        18.90                NaN      NaN   
3    Dan            Damages         1.83                NaN     1.83   
4    Dan            Damages         1.83                NaN     1.83   
5  Julie            Damages         1.83                NaN     1.83   
6    Dan      Problem solve         1.83                NaN      NaN   

   Problem solve  sweeper  
0            NaN      NaN  
1           9.70      NaN  
2            NaN     18.9  
3            NaN      NaN  
4            NaN      NaN  
5            NaN      NaN  
6           1.83      NaN 
anky
  • 74,114
  • 11
  • 41
  • 70
0
for i in range(len(df)):
    col = df.loc[i]['Function']
    df.at[i, col] = df.xs(i)['Total hours']
    print(col)

Try this ! Variable col finds the column for which you want to insert the value of Total hours.

Suraj
  • 2,253
  • 3
  • 17
  • 48