如何在 pandas 中做一系列的系列

编程入门 行业动态 更新时间:2024-10-28 03:25:34
本文介绍了如何在 pandas 中做一系列的系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说我有两个系列:a和b,

Say I have two series: a and b,

a = Series(None, index=['a','b','c']) b = Series('lol', index=['j','k','l'])

我想将b存储为a的元素之一,

I would like to store b as one of the elements of a,

a['a'] = b

但我明白了

ValueError: setting an array element with a sequence.

是否可以在熊猫系列中存储熊猫系列?我该怎么做?谢谢.

Is it possible to store a pandas series inside a pandas series? How can I do it? Thanks.

推荐答案

您可以使用 astype :

You can recast the dtype using the method astype:

In [11]: a = a.astype(object) In [12]: a['a'] = b In [13]: a Out[13]: a [lol, lol, lol] b NaN c NaN

构造a时(或者使用astype),可以强制dtype成为对象:

Alternatively (to using astype) when contructing a you can force the dtype to be object:

In [14]: a = Series(None, index=['a','b','c'], dtype=object)

出现此错误的原因是因为float64不允许Series,并且同样不允许字符串-尝试设置a['a'] = 'lol'会得到ValueError. /p>

The reason you are getting this error is because float64, doesn't allow a Series and similarly it doesn't allow strings - try to set a['a'] = 'lol' and you'll get a ValueError.

In [21]: a = Series(None, index=['a','b','c']) In [22]: a.dtype Out[22]: dtype('float64')

您可以在文档中阅读有关类型转换的更多信息.

更多推荐

如何在 pandas 中做一系列的系列

本文发布于:2023-10-07 17:14:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:系列   中做   如何在   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!