Home > other >  Flatten a dataframe with vector/list elements python
Flatten a dataframe with vector/list elements python

Time:04-08

Lets say I have a dataframe like this:

     A    B    C    Profile
0    1    4    4    [1,2,3,4]
1    2    4    5    [2,2,4,1]
3    2    4    5    [2,2,4,1]

How can I go about making it become this:

     A    B    C    Profile[0]   Profile[1]     Profile[2]     Profile[3]
0    1    4    4          1          2               3               4
1    2    4    5          2          2               4               1
3    2    4    5          2          2               4               1

I have tried this:

flat_list = [sublist for sublist in df['Profile']]
flat_df = pd.DataFrame(flat_list)
pd.concat([df.iloc[:,0:3], flat_df], axis=1)

BUT I have some NaN values and I need to retain the index for the flat list. This method just adds them all and moves all NaNs to the bottom instead of matching indices.

Ie i end up with this:

     A    B    C    Profile[0]   Profile[1]     Profile[2]     Profile[3]
0    1    4    4          1          2               3               4
1    2    4    5          2          2               4               1
2    NaN  NaN  NaN        2          2               4               1
3    2    4    5          NaN        NaN             NaN             NaN

TIA

CodePudding user response:

Change you line pass with the index

flat_df = pd.DataFrame(flat_list, index = df.index)

out = pd.concat([df.iloc[:,0:3], flat_df], axis = 1)
  • Related