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