Referring to this very popular question regarding groupby to dataframe. Unfortunately, I do not think this particular use case is the most useful, here's mine:
Suppose you have what could be a hierarchical dataset in a flattened form, e.g.
key val 0 'a' 2 1 'a' 1 2 'b' 3 3 'b' 4 what I wish to do is convert that dataframe to this structure
'a' 'b' 0 2 3 1 1 4 I thought this would be as simple as pd.DataFrame(df.groupby('key').groups) but it isn't.
How can I make this transformation?
pivot_tablein disguise; it allows counts of duplicate entries. See How to aggregate unique count with pandas pivot_table, Difference between groupby and pivot_table for pandas dataframes.