Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split multiple values in a row into multiple rows using pandas

Tags:

pandas

My data is as follows:

d = [{'id':1, 'a':10, 'b':10}, {"id":2,"a":20, "b":20}]  

The result I want is as follows:

res = [{'id': 1, 'prop': 'a', 'value': 10},
 {'id': 1, 'prop': 'b', 'value': 10},
 {'id': 2, 'prop': 'a', 'value': 20},
 {'id': 2, 'prop': 'b', 'value': 20}]

Since each row has two properties I want to split each row having the same id but different property and the corresponding value to that property

I am using iterrows as

import pandas as pd
df = pd.DataFrame(d)
l = []
for _, r in df.iterrows(): 
    for p in ['a','b']: 
        l.append({'id':r.id, 'prop':p, 'value':r[p]})

I get what I want but what I wish to know is whether there is any way to use purely pandas command to achieve my goal

like image 326
virupaksha Avatar asked Dec 20 '25 03:12

virupaksha


1 Answers

use pd.melt then use to_json with orient=records

convert string json to proper list format using json.loads

import json
d = [{'id':1, 'a':10, 'b':10}, {"id":2,"a":20, "b":20}]

df = pd.DataFrame(d)

res = pd.melt(df,id_vars=['id'],value_vars=['a','b'],var_name='prop')

json_res= json.loads(res.to_json(orient='records'))
[{"id":1,"prop":"a","value":10},{"id":2,"prop":"a","value":20},{"id":1,"prop":"b","value":10},{"id":2,"prop":"b","value":20}]
like image 145
tawab_shakeel Avatar answered Dec 21 '25 23:12

tawab_shakeel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!