2

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

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.