Python Pb 将 pandas.Series 列表转换为 pandas.Series 的 numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22292059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:42:01  来源:igfitidea点击:

Pb converting a list of pandas.Series into a numpy array of pandas.Series

pythonarraysnumpypandas

提问by deubNippon

I would like to convert a list of pandas.Seriesinto a numpy array of pandas.Series. But when I call the array constructor, it also converting my Series.

我想将一个列表转换pandas.Series为一个pandas.Series. 但是当我调用数组构造函数时,它也会转换我的系列。

>>> l = [Series([1,2,3]),Series([4,5,6])]
>>> np.array(l)
array([[1, 2, 3],
       [4, 5, 6]], dtype=int64)

My list is small (~10 elements), so for performances issuesI would like to avoid to create a pandas.DataFrame. Is there an easy workaround?

我的列表很小(约 10 个元素),因此对于性能问题,我想避免创建 pandas.DataFrame。有简单的解决方法吗?

Thanks in advance

提前致谢

回答by jme

You should set the dtypeof the array when you assign it:

您应该dtype在分配数组时设置它:

l = [pd.Series([1,2,3]),pd.Series([4,5,6])]
np.array(l, dtype=pd.Series)

Though it is raises the question: why do you want an ndarray of series, and not an ndarray of the contents of the series?

虽然它提出了一个问题:为什么你想要一个系列的 ndarray,而不是系列内容的 ndarray?