Ich habe eine Liste von numpy-Arrays, die ich in DataFrame konvertieren möchte. Jedes Array sollte eine Zeile des Datenrahmens sein.
Die Verwendung von pd.DataFrame () funktioniert nicht. Es wird immer der Fehler ausgegeben: ValueError: Muss eine 2D-Eingabe übergeben.
Gibt es einen besseren Weg, dies zu tun?
Dies ist mein aktueller Code:
list_arrays = array([[0, 0, 0, 1, 0, 0, 0, 0, 00]], dtype=uint8), array([[0, 0, 3, 2, 0, 0, 0, 0, 00]], dtype=uint8)]
d = pd.DataFrame(list_of_arrays)
ValueError: Must pass 2-d input
Option 1:
In [143]: pd.DataFrame(np.concatenate(list_arrays))
Out[143]:
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
Option 2:
In [144]: pd.DataFrame(list(map(np.ravel, list_arrays)))
Out[144]:
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
Warum bekomme ich:
ValueError: Must pass 2-d input
Ich denke, pd.DataFrame()
versucht es wie folgt in NDArray umzuwandeln:
In [148]: np.array(list_arrays)
Out[148]:
array([[[0, 0, 0, 1, 0, 0, 0, 0, 0]],
[[0, 0, 3, 2, 0, 0, 0, 0, 0]]], dtype=uint8)
In [149]: np.array(list_arrays).shape
Out[149]: (2, 1, 9) # <----- NOTE: 3D array
pd.DataFrame(sum(map(list, list_arrays), []))
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
pd.DataFrame(np.row_stack(list_arrays))
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
Sie können pd.Series
verwenden
pd.Series(l).apply(lambda x : pd.Series(x[0]))
Out[294]:
0 1 2 3 4 5 6 7 8
0 0 0 0 1 0 0 0 0 0
1 0 0 3 2 0 0 0 0 0
Hier ist ein Weg.
import numpy as np, pandas as pd
lst = [np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=int),
np.array([[0, 0, 3, 2, 0, 0, 0, 0, 0]], dtype=int)]
df = pd.DataFrame(np.vstack(lst))
# 0 1 2 3 4 5 6 7 8
# 0 0 0 0 1 0 0 0 0 0
# 1 0 0 3 2 0 0 0 0 0