ich eine 2D Numpy Array haben, Ich mag würde in einer Pandas Serie setzen (kein Datenrahmen):Setzen Sie ein 2D-Array in eine Pandas Serie
>>> 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.]])
Aber das wirft einen Fehler:
>>> 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
Es ist möglich, mit einem Hack:
>>> 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]
gibt es einen besseren Weg?
standardmäßig Pandas die Form der bekommt np array und weist DataFrame entsprechend zu. Also musst du die Form deines np-Arrays täuschen ... Was du "hackst", wenn auch nur eine Zeile nach der anderen. – Kartik
Gibt es Überlegungen, wie Sie jede Zeile in einer Liste mithilfe einer Matrixoperation umbrechen? – zemekeneng
Nur aus Neugier, warum willst du das? –