0

I have dataframe df as follows:

   API     sp_name               sp_input_params
getData   analytics.sp_1        {'req_url_query_params': [['@scroll_index', 'index']], 
                                 'req_body_params': [['@event_type_id', 'event_type_id']]}
getParam  analytics.sp_2        {'req_url_query_params': [['@athlete_guid', 'athlete_guid']], 
                                 'req_body_params': []}

Now I want to normalize the field sp_input_params. One of the way to achieve this as

df_final = pd.json_normalize(df['sp_input_params'])

But the above wont keep the other two columns viz API and sp_name. And I want to keep them along with the normalized one.

Any clue on this?

pythondumb
  • 1,187
  • 1
  • 15
  • 30

1 Answers1

0

I think you can use if default index in df.index:

df_final = df.join(pd.json_normalize(df.pop('sp_input_params')))

If not, use:

df_final = df.join(pd.json_normalize(df.pop('sp_input_params')).set_index(df.index))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • What is this 'pop thing? I didn't know about the same. Secondly after extracting the JSON I am getting data in lists. Like `[['@scroll_index', 'index']]`. Is it possible to apply `lambda` on the resultant dataframe so as to make them `unlisted`? Third, what if I use more than 1 columns inside `pop`? – pythondumb Oct 04 '21 at 08:03
  • Here is used [`DataFrame.pop`](http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pop.html) for used nad drop column from original. `Secondly after extracting the JSON I am getting data in lists` - What is expected ouput? No, multiple `columns` cannot be in `pop` – jezrael Oct 04 '21 at 08:08
  • Expected output in plain string. And I have done that using `apply`. So how can I add multiple columns having JSON strings in it? – pythondumb Oct 04 '21 at 08:16
  • @pythondumb - Another idea is use [this](https://stackoverflow.com/questions/35491274/split-a-pandas-column-of-lists-into-multiple-columns/35491399#35491399) solution – jezrael Oct 04 '21 at 08:17
  • @pythondumb - Is possible add columns with json data to question like `pd.DataFrame(data)` ? And what is expected ouput from input data? – jezrael Oct 04 '21 at 09:07