Ich habe dies einen Datenrahmen wie folgt:
| Spalte1 | col2 | col3 |
|---|---|---|
| UNS | ein | ein |
| UNS | ein | 2 |
| UNS | 2 | ein |
| NL | ein | ein |
| UNS | 2 | 2 |
| DK | ein | ein |
Lösung des Problems
Hier ist eine Option mit einem verschachtelten groupby:
out = df.groupby('col1').apply(lambda g: g.groupby('col2')['col3'].agg(list).to_dict()).to_dict()
Ausgabe:
{'DK': {1: [1]}, 'NL': {1: [1]}, 'US': {1: [1, 2], 2: [1, 2]}}
Keine Kommentare:
Kommentar veröffentlichen