I have a Pandas DataFrame that looks like this (currently without an index other than the in-built row index, but if it's easier to add indexes to "Person" and "Car", that's fine too):
before = pd.DataFrame({
'Email': ['john@example.com','mary@example.com','jane@example.com','john@example.com','mary@example.com'],
'Person': ['John','Mary','Jane','John','Mary'],
'Car': ['Ford','Toyota','Nissan','Nissan','Ford']
})
I'd like to re-shape it to look like this:
after = pd.DataFrame({
'Person': ['John','Mary','Jane'],
'Email': ['john@example.com','mary@example.com','jane@example.com'],
'Ford': [True,True,False],
'Nissan': [True,False,True],
'Toyota': [False,True,False]
})
Note that John has owned both a Ford and a Nissan, Mary has owned a Ford and a Toyota, and Paul has stuck with his trusty Nissan.
I've tried various permutations of stacking a multi-indexed DataFrame, grouping, pivoting -- I can't seem to figure out how to take the value from the "Car" column and transpose it to a new column with the value "True", merging people together by, say, their name.