gpt4 book ai didi

python - 将 Pandas 列值设置为数组

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:40 24 4
gpt4 key购买 nike

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

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

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

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

问题是当我尝试这样做时:

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

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

最佳答案

不推荐您正在尝试的操作。1 Pandas 并非设计用于连续保存列表。话虽如此,您可以明确定义一个系列并通过 update 分配。或 loc .备注at仅用于获取或设置一个单个值,而不是您的情况下的多个值。

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 更多信息 ( 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.

关于python - 将 Pandas 列值设置为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53575958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com