Expanding DataFrame which has List within Tuple - Pandas python

Viewed 55

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

1 Answers

The idea here is to make 2 columns out of 'exploded_features' and then join that with your original dataframe then explode both columns:

use join()+DataFrame()+agg()/apply():

sample_df=sample_df.join(pd.DataFrame(sample_df.pop('exploded_features').tolist()))
sample_df=sample_df.agg(pd.Series.explode)
sample_df.columns=['id','exploded_features_1','exploded_features_2']

OR

specify the column names in the dataframe itself:

s=pd.DataFrame(sample_df.pop('exploded_features').tolist(),columns=['exploded_features_1','exploded_features_2'])
sample_df=sample_df.join(s).agg(pd.Series.explode)

Note: if needed use sample_df=sample_df.reset_index(drop=True)

output of sample_df:

    id  exploded_features_1     exploded_features_2
0   1   a1                      b1
0   1   a2                      b2
0   1   a3                      b3
1   2   a4                      b4
1   2   a5                      b5
1   2   a6                      b6
2   3   a7                      b7
2   3   a8                      b8
2   3   a9                      b9
Related