Home > Software engineering >  Pandas assign values from array in row to columns
Pandas assign values from array in row to columns

Time:08-04

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,)

CodePudding user response:

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

CodePudding user response:

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

  • Related