将2d阵列放入Pandas系列中 [英] Put a 2d Array into a Pandas Series
本文介绍了将2d阵列放入Pandas系列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2D Numpy数组,我想放入一个熊猫系列(不是DataFrame)中:
I have a 2D Numpy array that I would like to put in a pandas Series (not a DataFrame):
>>> import pandas as pd
>>> import numpy as np
>>> a = np.zeros((5, 2))
>>> a
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
但这会引发错误:
>>> s = pd.Series(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/miniconda/envs/pyspark/lib/python3.4/site-packages/pandas/core/series.py", line 227, in __init__
raise_cast_failure=True)
File "/miniconda/envs/pyspark/lib/python3.4/site-packages/pandas/core/series.py", line 2920, in _sanitize_array
raise Exception('Data must be 1-dimensional')
Exception: Data must be 1-dimensional
有可能被黑客入侵
>>> s = pd.Series(map(lambda x:[x], a)).apply(lambda x:x[0])
>>> s
0 [0.0, 0.0]
1 [0.0, 0.0]
2 [0.0, 0.0]
3 [0.0, 0.0]
4 [0.0, 0.0]
有更好的方法吗?
推荐答案
那么,您可以使用numpy.ndarray.tolist
函数,如下所示:
Well, you can use the numpy.ndarray.tolist
function, like so:
>>> a = np.zeros((5,2))
>>> a
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> a.tolist()
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
>>> pd.Series(a.tolist())
0 [0.0, 0.0]
1 [0.0, 0.0]
2 [0.0, 0.0]
3 [0.0, 0.0]
4 [0.0, 0.0]
dtype: object
完成类似结果的更快方法是简单地执行pd.Series(list(a))
.这将产生一系列的numpy数组,而不是Python列表,因此应该比返回Python列表列表的a.tolist
更快.
A faster way to accomplish a similar result is to simply do pd.Series(list(a))
. This will make a Series of numpy arrays instead of Python lists, so should be faster than a.tolist
which returns a list of Python lists.
这篇关于将2d阵列放入Pandas系列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文