1

I have a dataframe with nested json as column.

df.depth
0     {'buy': [{'quantity': 51, 'price': 2275.85, 'o...
1     {'buy': [{'quantity': 1, 'price': 2275.85, 'or...
2     {'buy': [{'quantity': 1, 'price': 2275.85, 'or..

inside each row have 5 depths of buy sell

df.depth[0]
{'buy': [{...}, {...}, {...}, {...}, {...}], 'sell': [{...}, {...}, {...}, {...}, {...}]}

real json structure is as below

{'buy': [{'quantity': 51, 'price': 2275.85, 'orders': 2}, {'quantity': 38, 'price': 2275.8, 'orders': 2}, {'quantity': 108, 'price': 2275.75, 'orders': 3}, {'quantity': 120, 'price': 2275.7, 'orders': 2}, {'quantity': 6, 'price': 2275.6, 'orders': 1}], 'sell': [{'quantity': 353, 'price': 2276.95, 'orders': 1}, {'quantity': 29, 'price': 2277.0, 'orders': 2}, {'quantity': 54, 'price': 2277.1, 'orders': 2}, {'quantity': 200, 'price': 2277.2, 'orders': 1}, {'quantity': 4, 'price': 2277.25, 'orders': 1}]}

i want to explode this in to something like this

Required Output:

   depth.buy.quantity1  df.depth.buy.price1 ... depth.sell.quantity1 depth.sell.price1....
0      51                2275.85....                    353          2276
1      1                 2275.85....                    352          2276

how to do it ?

Edit:

for help i have added demo dataframe:

a={'buy': [{'quantity': 51, 'price': 2275.85, 'orders': 2}, {'quantity': 38, 'price': 2275.8, 'orders': 2}, {'quantity': 108, 'price': 2275.75, 'orders': 3}, {'quantity': 120, 'price': 2275.7, 'orders': 2}, {'quantity': 6, 'price': 2275.6, 'orders': 1}], 'sell': [{'quantity': 353, 'price': 2276.95, 'orders': 1}, {'quantity': 29, 'price': 2277.0, 'orders': 2}, {'quantity': 54, 'price': 2277.1, 'orders': 2}, {'quantity': 200, 'price': 2277.2, 'orders': 1}, {'quantity': 4, 'price': 2277.25, 'orders': 1}]}
c=dict()
c['depth'] = a
df = pd.DataFrame([c,c])

1 Answer 1

1

You could try concat:

df = pd.concat([pd.concat([pd.DataFrame(x, index=[0]) for x in i], axis=1) for i in pd.json_normalize(df['depth'])['buy'].tolist()], ignore_index=True)
print(df)

Output:

   quantity    price  orders  quantity   price  orders  ...  quantity   price  orders  quantity   price  orders
0        51  2275.85       2        38  2275.8       2  ...       120  2275.7       2         6  2275.6       1
1        51  2275.85       2        38  2275.8       2  ...       120  2275.7       2         6  2275.6       1

[2 rows x 15 columns]
Sign up to request clarification or add additional context in comments.

5 Comments

it comes one after another i want in one row
why it creates 75 columns it should create for buy 5x3 = 15columns
i have added demo dataframe can u check
@SurajS Edited my answer pls check it out
its not working it gives 3 columns but needs 15 buy columns and 15 sell columns with only 2 rows as per demo data given

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.