I am working on a Pandas dataframe problem that stored it's results in a tuple list format. In essence, each 'tuple' represents an expanded DataFrame for the specific ID.
I understand that I have to expand the 'exploded_features' column for each ID and merge all the separate dataframes together. However, I'm wondering what is the best solution to this problem.
Would appreciate your thoughts!
sample_df = pd.DataFrame({'id':[1,2,3],
'exploded_features':[(['a1','a2','a3'],['b1','b2','b3']),
(['a4','a5','a6'],['b4','b5','b6']),
(['a7','a8','a9'],['b7','b8','b9'])]})
-------------------------------------------------
sample_df:
id exploded_features
0 1 ([a1, a2, a3], [b1, b2, b3])
1 2 ([a4, a5, a6], [b4, b5, b6])
2 3 ([a7, a8, a9], [b7, b8, b9])
The following is my intended results.
sample_df_ideal = pd.DataFrame({'id':[1,1,1,2,2,2,3,3,3],
'exploded_features_1':['a1','a2','a3','a4','a5','a6','a7','a8','a9'],
'exploded_featuers_2':['b1','b2','b3','b4','b5','b6','b7','b8','b9']})
sample_df_ideal:
id exploded_features_1 exploded_featuers_2
0 1 a1 b1
1 1 a2 b2
2 1 a3 b3
3 2 a4 b4
4 2 a5 b5
5 2 a6 b6
6 3 a7 b7
7 3 a8 b8
8 3 a9 b9