I have tow columns in my data frame that have multiple dictionarise in it and i whant to expand it into multiple coulmns but the proplem when i use explode seriese it mismatch
example:
Column A | Column B | Columns C |
---|---|---|
Cell 1 | {"a":0.5} {"b":0.5} |
{"d1":0.25}{"d2":0.25}{"d3":0.25}{"d4":0.25} |
Cell 2 | {"c":1.0} |
{"t1":0.5} {"t2":0.5} |
desirable output:
Column A | Column B1 | Columns B2 | Columns C1 | Columns C2 |
---|---|---|---|---|
Cell 1 | "a" | 0.5 | "d1" | 0.25 |
Cell 1 | "a" | 0.5 | "d2" | 0.25 |
Cell 1 | "b" | 0.5 | "d3" | 0.25 |
Cell 1 | "b" | 0.5 | "d4" | 0.25 |
Cell 2 | "c" | 1.0 | "t1" | 0.5 |
Cell 2 | "c" | 1.0 | "t2" | 0.5 |
i used to use
df1 = (
df.assign(Columns B2 =lambda df: df.Columns B.apply(lambda x: x.values()))
.reset_index()
.apply(pd.Series.explode)
)
output = (
df1.assign(Columns C2 =lambda df: df.Columns C.apply(lambda x: x.values()))
.reset_index()
.apply(pd.Series.explode)
)
the problem is that it mismatches the rows something in column C1 supposed to be in row 2 go to 3