Set Pandas column values to an array

让人想犯罪 __ 提交于 2019-12-01 21:30:28

What you're attempting is not recommended.1 Pandas is not designed to hold list in series. Having said this, you can define a series explicitly and assign via update or loc. Note at is used to get or set a single value only, not multiple values as in your case.

a = [5, 5, 5]
indices = [0, 2]

df['col3'].update(pd.Series([a]*len(indices), index=indices))

# alternative:
# df.loc[indices, 'col3'] = pd.Series([a]*len(indices), index=indices)

print(df)

   col1  col2       col3
0     2     5  [5, 5, 5]
1     4     3          5
2     6     2  [5, 5, 5]

1 For more information (source):

Don't do this. Pandas was never designed to hold lists in series / columns. You can concoct expensive workarounds, but these are not recommended.

The main reason holding lists in series is not recommended is you lose the vectorised functionality which goes with using NumPy arrays held in contiguous memory blocks. Your series will be of object dtype, which represents a sequence of pointers, much like list. You will lose benefits in terms of memory and performance, as well as access to optimized Pandas methods.

See also What are the advantages of NumPy over regular Python lists? The arguments in favour of Pandas are the same as for NumPy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!