Reputation: 1891
Lets say I have this dataset:
{
"_id" : 43,
"userId" : 5,
"Ids" : [
"10",
"59",
"1165",
"1172"
],
"roles" : [
"5f84d38", "6245d38"
]
}
Current Dataframe:
_id userId Ids roles
43 5 [10,59,1165,1172] [5f84d38,6245d38]
How do I explode both lists so that it will give this output below.
Desired Dataframe:
_id userId Ids roles
43 5 10 5f84d38
43 5 59 5f84d38
43 5 1165 5f84d38
43 5 1172 5f84d38
43 5 10 6245d38
43 5 59 6245d38
43 5 1165 6245d38
43 5 1172 6245d38
I checked to see whether there were any similar questions posted, but all of the solutions are referring to lists which have the same amount of items within or just one single list column, but my dataset contains 2 list columns both of different lengths.
Upvotes: 0
Views: 220
Reputation: 1950
Try this:
import pandas as pd
d = {
"_id" : 43,
"userId" : 5,
"Ids" : [
"10",
"59",
"1165",
"1172"
],
"roles" : [
"5f84d38", "6245d38"
]
}
df = pd.DataFrame(columns=d.keys())
rows = []
for role in d['roles']:
for _id in d['Ids']:
df = df.append({"_id" :d["_id"], "userId": d["userId"], "Ids":_id, "roles": role}, ignore_index=True)
Upvotes: 1