【问题标题】:Pandas assign values from array in row to columns熊猫将行中数组中的值分配给列
【发布时间】:2022-08-04 00:49:31
【问题描述】:

我有关注熊猫

    | 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] |

我需要 DataFrame 看起来像

        | 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 是带有(300,) 的 np.array

    标签: python pandas


    【解决方案1】:

    尝试这个

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

    这会给你想要的df

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2020-04-13
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多