Here is a pandas DataFrame I would like to manipulate:
import pandas as pd data = {\"grouping\": [\"item1\", \"item1\", \"item1\", \"item2\", \"item2\", \"
Use set_index and unstack:
set_index
unstack
df = df.set_index(['grouping','labels']).unstack().rename_axis(None) df.columns = df.columns.droplevel() print(df)
Output:
labels A B C D item1 5 1 8 None item2 3 731 189 9