Pandas - normalize Json list
Use DataFrame.explode
on column bids
then create a new dataframe from the dictionaries in exploded bids
column and use DataFrame.join
to join
it with df
:
df = data.explode('bids').dropna(subset=['bids']).reset_index(drop=True)df = df.join(pd.DataFrame(df.pop('bids').tolist()))
Result:
print(df) id price quantity0 1 606 281 1 588 292 1 513 333 3 7143 154 3 68 915 3 6849 12
Adding another approach with np.repeat
and np.concatenate
with json_normalize
out = pd.io.json.json_normalize(np.concatenate(data['bids']))out.insert(0,'id',np.repeat(data['id'],data['bids'].str.len()).to_numpy())
Or you can also use np.hstack
as @Shubham mentions instead of np.concatenate
:
out = pd.io.json.json_normalize(np.hstack(data['bids']))
print(out) id price quantity0 1 606 281 1 588 292 1 513 333 3 7143 154 3 68 915 3 6849 12