将Pandas列值设置为数组 [英] Set Pandas column values to an array

查看:394
本文介绍了将Pandas列值设置为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题: 我有一个像这样的数据框:

I have the following problem: I have a dataframe like this one:

   col1   col2   col3
0   2       5      4
1   4       3      5
2   6       2      7 

现在我有一个数组,例如a = [5,5,5],我想将此数组插入col3中,但只插入特定的行中(比如说0和2),并获得类似的结果:

Now I have an array for example a = [5,5,5] and i want to insert this array in col3 but only in specific rows (let's say 0 and 2) and obtain something like that:

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

问题在于,当我尝试这样做时:

The problem is that when I try to do:

 zip_df.at[[0,2],'col3'] = a 

我收到以下错误ValueError: Must have equal len keys and value when setting with an ndarray.我该如何解决这个问题?

I receive the following error ValueError: Must have equal len keys and value when setting with an ndarray. How can I solve this problem?

推荐答案

不建议您尝试使用什么方法. 1 Pandas并非旨在容纳一系列列表.话虽如此,您可以显式定义一个系列并通过 update loc .注意 at 用于获取或仅设置单个值,而不是您所设置的多个值.

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 有关更多信息( ):


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.

不推荐连续举牌的主要原因是您输了 向量功能,该功能与使用连续内存块中保存的NumPy数组一起使用.您的系列将是 object dtype,它表示指针序列,与list相似.你会输的 在内存和性能以及访问优化的Pandas方法方面都有好处.

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.

另请参见 NumPy相对于NumPy有哪些优势常规Python 列表? 支持熊猫的论点与针对NumPy的论点相同.

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

这篇关于将Pandas列值设置为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆