Converting Array Of Arrays Into Flattened Dataframe
Got a pandas dataframe with the below structure 0 [{'review_id': 4873356, 'rating': '5.0'}, {'review_id': 4973356, 'rating': '4.0'}] 1 [{'review_id': 4635892, 'rating': '5.0'
Solution 1:
You wind up getting an array of lists of dicts so need:
import pandas as pd
pd.DataFrame([x foryin df1.values forxin y])
rating review_id
05.0487335614.0497335625.0463589233.04645839
Or if willing to use itertools
:
from itertools import chain
pd.DataFrame(chain.from_iterable(df1.values.ravel()))
Solution 2:
1st unnesting , then re build your dataframe (assuming you have columns name 0)
pd.DataFrame(unnesting(df,[0])[0].values.tolist())
Out[61]:
rating review_id
05.0487335614.0497335625.0463589233.04645839
defunnesting(df, explode):
idx=df.index.repeat(df[explode[0]].str.len())
df1=pd.concat([pd.DataFrame({x:np.concatenate(df[x].values)} )for x in explode],axis=1)
df1.index=idx
return df1.join(df.drop(explode,1),how='left')
Post a Comment for "Converting Array Of Arrays Into Flattened Dataframe"