-2

I have the following dataframe:

      averageNumberOfOperationsPerPath  id   get  post  put  delete  
0                             1.285714           84  12.0   4.0  1.0     1.0   
1                             1.266667           84  13.0   4.0  1.0     1.0   
2                             1.266667           84  13.0   4.0  1.0     1.0   
3                             3.333333          124   3.0   1.0  2.0     1.0   
4                             3.333333          124   3.0   1.0  2.0     1.0     

I need all the different columns get, post, put, delete and there are a few more(I haven't mentioned here, the dataframe becomes too big for readability), under one column methods and other column value with their respective values.

Any help on how can I merge them to get the desired output while keeping the number of rows same.

1 Answers1

2

I think you can use stack method:

df= (df
     .set_index(['averageNumberOfOperationsPerPath','api_spec_id'])
     .stack()
     .reset_index()
     .rename(columns={'level_2': 'methods', 0:'values'}))

print(df)

   averageNumberOfOperationsPerPath  api_spec_id methods  values
0                          1.285714           84     get    12.0
1                          1.285714           84    post     4.0
2                          1.285714           84     put     1.0
3                          1.285714           84  delete     1.0
4                          1.266667           84     get    13.0



YOLO
  • 20,181
  • 5
  • 20
  • 40