将二维数组分配给panda datafram

2024-06-16 11:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将二维数组分解成一维数组,并将其分配给panda数据帧。需要帮忙吗

This is my panda data frame. 

 Id       Dept            Date

100    Healthcare        2007-01-03
100    Healthcare        2007-01-10
100    Healthcare        2007-01-17

Two dimensional array looks like

array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])

输出为

 Id       Dept            Date                vect

100    Healthcare        2007-01-03          [10, 20, 30]
100    Healthcare        2007-01-10          [40, 50, 60]
100    Healthcare        2007-01-17          [70, 80, 90]

Tags: 数据iddatadateismy数组this
1条回答
网友
1楼 · 发布于 2024-06-16 11:14:39

可以通过使用tolistarray转换为list来实现

df['vect']=ary.tolist()
df
Out[281]: 
    Id        Dept        Date          vect
0  100  Healthcare  2007-01-03  [10, 20, 30]
1  100  Healthcare  2007-01-10  [40, 50, 60]
2  100  Healthcare  2007-01-17  [70, 80, 90]

相关问题 更多 >