Pivot vs unstack?

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
horstel_horst
User
Beiträge: 18
Registriert: Montag 27. September 2021, 19:09

Ich habe einen pandas Dataframe und möchte den in ein anderes Format überführen.

Code: Alles auswählen

import pandas as pd
ids = ['a','a','b','b']
item = ['apple','apfel','orange','organe']
df_have = pd.DataFrame({'id':ids,'item':item})
So sieht der Datafram aus. Ich benötige aber folgendes Format:

Code: Alles auswählen

ids = ['a','b']
item_1 = ['apple','orange']
item_2 =  ['apfel','orange']
df_want = pd.DataFrame({'id':ids,'item_1':item_1,'item_2':item_2})
Wie löse ich das am besten?
Antworten