I am trying to take out keys and values from json to separate rows in pandas
I have:
|---------------------|------------------|
| session | scoring |
|---------------------|------------------|
| session1 | {id1:scoring1, |
| | id2:scoring2, |
| | id3:scoring3} |
|---------------------|------------------|
| session2 | {id4:scoring4, |
| | id5:scoring5} |
|---------------------|------------------|
I would like to get:
|---------------------|------------------|---------------------|------------------|
| session | scoring | id | score |
|---------------------|------------------|---------------------|------------------|
| session1 | {id1:scoring1, | id1 | score1 |
| | id2:scoring2, | | |
| | id3:scoring3} | | |
|---------------------|------------------|---------------------|------------------|
| session1 | {id1:scoring1, | id2 | score2 |
| | id2:scoring2, | | |
| | id3:scoring3} | | |
|---------------------|------------------|---------------------|------------------|
| session1 | {id1:scoring1, | id3 | score3 |
| | id2:scoring2, | | |
| | id3:scoring3} | | |
|---------------------|------------------|---------------------|------------------|
| session2 | {id4:scoring4, | id4 | score4 |
| | id5:scoring5} | | |
|---------------------|------------------|---------------------|------------------|
| session2 | {id4:scoring4, | id5 | score5 |
| | id5:scoring5} | | |
|---------------------|------------------|---------------------|------------------|
The code i used: (Iterating the rows and jsons, if id is first in json then put it in adjacent cell else create new row and append to df)
append_index = df.shape[0]
for index, row in df.iterrows():
append_now = False
for key, val in row['scoring'].items():
if append_now:
row['id'] = key
row['score'] = val
df.loc[append_index] = row
append_index += 1
else:
df.loc[index,'id'] = key
df.loc[index, 'score'] = val
append_now = True
The problem is that df consists of 6+ mlm rows and to iterate only 20rows it takes half an hour. But when I limit first 1k rows it works well