Home > Blockchain >  Extraction of array list values in pandas dataframe
Extraction of array list values in pandas dataframe

Time:11-14

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?

CodePudding user response:

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'}
  • Related