0

I have following pandas

    | TEXT | FEATURE|
    | test | [0.2, 0.3, 0.4 .... n] |
    | test | [0.2, 0.3, 0.4 .... n] |
    |test2 | [0.2, 0.3, 0.4 .... n] |
    |test2 | [0.2, 0.3, 0.4 .... n] |

I need DataFrame to looks like

        | TEXT | FEATURE 1| FEATURE 2 | FEATURE 3 | FEATURE n |
        | test |  0.2,    |  0.3      |   0.4     |     n     |
        | test |  0.2,    |  0.3      |   0.4     |     n     |
        |test2 |  0.2,    |  0.3      |   0.4     |     n     |
        |test2 |  0.2,    |  0.3      |   0.4     |     n     |

FEATURE is np.array with (300,)

0

2 Answers 2

2

here is one way to do it

drop the parenthesis and split on comma value, then concat with resulting column the TEXT column

df2=pd.concat([df['TEXT'],
           df['FEATURE'].str.replace(r'[\[\]]','', regex=True).str.split(',', expand=True).add_prefix('Feature_')], 
              axis=1)
df2 
    TEXT    Feature_0   Feature_1   Feature_2   Feature_3
0   test    0.2     0.3     0.4     ....n
1   test    0.2     0.3     0.4     ....n
2   test2   0.2     0.3     0.4     ....n
3   test2   0.2     0.3     0.4     ....n
Sign up to request clarification or add additional context in comments.

1 Comment

I like yours it adapts to the size of the given rows list
1

Try this

ft = pd.DataFrame(df['Feature'].to_list(), columns= [f'FEATURE {i}' for i in range(len(df['Feature'][0]))])

pd.concat([df.drop('Feature',axis=1),ft])

This shall give your wanted df

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.