[FIXED] von der Liste der Liste im Pandas-Datenrahmen zu einem neuen Listensatz mit mehreren Spalten in Pandas

Ausgabe

Ich habe diese Werte im Datensatz in einer Pandas-Datenrahmenspalte

col1
[[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]]
[[13,14],[15,16],[17,18],[19,20],[21,22],[23,24]]

Ich möchte 6 Elemente als Liste in neuen Spalten als Zeilen erhalten.

Dies sind die Spalten, die ich erhalten möchte.

col2                    col3
[1,3,5,7,9,11]          [2,4,6,8,10,12]
[13,15,17,19,21,23]     [14,16,18,20,22,24]

Lösung

Sie können ein Listenverständnis und den DataFrame-Konstruktor verwenden:

df[['col2', 'col3']] = pd.DataFrame([list(map(list, zip(*l))) for l in df['col1']])

Ein anderer Ansatz mit :

a = np.dstack(df['col1'].to_numpy())
df['col2'] = a[:,0].T.tolist()
df['col3'] = a[:,1].T.tolist()

Ausgabe:

                                                           col1                      col2                      col3
0           [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]       [1, 3, 5, 7, 9, 11]      [2, 4, 6, 8, 10, 12]
1  [[13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]]  [13, 15, 17, 19, 21, 23]  [14, 16, 18, 20, 22, 24]


Beantwortet von –
mozway


Antwort geprüft von –
Candace Johnson (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

[FIXED] seaborn.boxplot und großformatiger Datenrahmen

Ausgabe Ich habe so eine DataFrame: Ich habe diese beiden Anweisungen nacheinander ausprobiert: sns.boxplot([dataFrame.mean_qscore_template,dataFrame.mean_qscore_complement,dataFrame.mean_qscore_2d]) sns.boxplot(x = "mean_qscore_template", y=…