Issue
I have the following dataframe:
averageNumberOfOperationsPerPath api_spec_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.
I have been looking at different ways it could be done, but not understanding, this might be a very trivial question, but I am bit new to this. Any suggestions or guidance will be helpful!
Solution
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
Answered By - YOLO
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.