I have the following dataframe where col2 is a dictionary with a list of tuples as values. The keys are consistantly 'added' and 'deleted' in the whole dataframe.
Input df
| col1 | col2 |
|---|---|
| value1 | {'added': [(59, 'dep1_v2'), (60, 'dep2_v2')], 'deleted': [(59, 'dep1_v1'), (60, 'dep2_v1')]} |
| value 2 | {'added': [(61, 'dep3_v2')], 'deleted': [(61, 'dep3_v1')]} |
Here's a copy-pasteable example dataframe:
jsons = ["{'added': [(59, 'dep1_v2'), (60, 'dep2_v2')], 'deleted': [(59, 'dep1_v1'), (60, 'dep2_v1')]}",
"{'added': [(61, 'dep3_v2')], 'deleted': [(61, 'dep3_v1')]}"]
df = pd.DataFrame({"col1": ["value1", "value2"], "col2": jsons})
edit
col2 directly comes from the diff_parsed field of pydriller output
I want to "explode" col2 so that I obtain the following result:
Desired output
| col1 | number | added | deleted |
|---|---|---|---|
| value1 | 59 | dep1_v2 | dep1_v1 |
| value1 | 60 | dep2_v2 | dep2_v1 |
| value2 | 61 | dep3_v2 | dep3_v1 |
So far, I tried the following:
df = df.join(pd.json_normalize(df.col2))
df.drop(columns=['col2'], inplace=True)
The above code is simplified. I first manipulate the column to convert to proper json. It was in an attempt to first explode on 'added' and 'deleted' and then try to play around with the format to obtain what I want...but the list of tuples is not preserved and I obtain the following:
| col1 | added | deleted |
|---|---|---|
| value1 | 59, dep1_v2, 60, dep2_v2 | 59, dep1_v1, 60, dep2_v1 |
| value2 | 61, dep3_v1 | 61, dep3_v2 |
Thanks