0

I have a dataframe which looks like this:

df= pd.DataFrame({'methods': {0: {'get': 12,
   'post': 4,
   'put': 1,
   'delete': 1,
   'patch': 0,
   'head': 0,
   'options': 0,
   'trace': 0,
   'connect': 0},
  1: {'get': 13,
   'post': 4,
   'put': 1,
   'delete': 1,
   'patch': 0,
   'head': 0,
   'options': 0,
   'trace': 0,
   'connect': 0},
  2: {'get': 13,
   'post': 4,
   'put': 1,
   'delete': 1,
   'patch': 0,
   'head': 0,
   'options': 0,
   'trace': 0,
   'connect': 0},
  3: {'get': 3,
   'post': 1,
   'put': 2,
   'delete': 1,
   'patch': 1,
   'head': 0,
   'options': 0,
   'trace': 0,
   'connect': 0,
   'parameters': {'$numberDouble': 'NaN'}},
  4: {'get': 3,
   'post': 1,
   'put': 2,
   'delete': 1,
   'patch': 1,
   'head': 0,
   'options': 0,
   'trace': 0,
   'connect': 0,
   'parameters': {'$numberDouble': 'NaN'}}},
 'averageNumberOfOperationsPerPath': {0: 1.2857142857142851,
  1: 1.266666666666666,
  2: 1.266666666666666,
  3: 3.333333333333333,
  4: 3.333333333333333},
 'api_spec_id': {0: 84, 1: 84, 2: 84, 3: 124, 4: 124}})

I want to extract the values for column methods in different dataframes, like get, post, put with their values underneath. What would be the best way to achieve this?

I tried using eval() function and something like this `

df1 = df.pop('methods').str.strip('{').str.split(':',expand=True).astype(float)

but did not work either. Any suggestions what I should be using instead?

1 Answer 1

1

You can try:

df = pd.concat([df, df['methods'].agg(pd.Series)], axis=1) 

Output:

methods                                                  get    post put    delete  patch   head    options trace   connect parameters
0   {'get': 12, 'post': 4, 'put': 1, 'delete': 1, ...   12.0    4.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 NaN
1   {'get': 13, 'post': 4, 'put': 1, 'delete': 1, ...   13.0    4.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 NaN
2   {'get': 3, 'post': 1, 'put': 2, 'delete': 1, '...   3.0 1.0 2.0 1.0 1.0 0.0 0.0 0.0 0.0 {'$numberDouble': 'NaN'}
3   {'get': 3, 'post': 6, 'put': 0, 'delete': 2, '...   3.0 6.0 0.0 2.0 2.0 0.0 0.0 0.0 0.0 {'$numberDouble': 'NaN'}
4   {'get': 4, 'post': 1, 'put': 3, 'delete': 1, '...   4.0 1.0 3.0 1.0 0.0 0.0 0.0 0.0 0.0 NaN
5   {'get': 3, 'post': 3, 'put': 3, 'delete': 3, '...   3.0 3.0 3.0 3.0 0.0 0.0 0.0 0.0 0.0 {'$numberDouble': 'NaN'}
Sign up to request clarification or add additional context in comments.

3 Comments

This gives me an error saying array not defined, my bad, i updated how my the column looks like in my entire dataframe, the array field comes when i print out the unique values for methods
What will be the modification in the code to extract them now?
You only need the concat step then. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.