The sample data looks like this:
d = pd.DataFrame({'name': ['Adam', 'Adam', 'Bob', 'Bob', 'Craig'],
'number': [111, 222, 333, 444, 555],
'type': ['phone', 'fax', 'phone', 'phone', 'fax']})
name number type
------ ------ -----
Adam 111 phone
Adam 222 fax
Bob 333 phone
Bob 444 phone
Craig 555 fax
I am trying to convert the numbers (phone and fax) to a wide format, the ideal output:
name fax phone
---- ----- -----
Adam 222.0 111.0
Bob NaN 333.0
Bob NaN 444.0
Craig 555.0 NaN
When I tried to use the pivot
method and run the following code:
p = d.pivot(index='name', columns = 'type', values='number').reset_index()
I received the error ValueError: Index contains duplicate entries, cannot reshape
due to the fact that Bob has two phone numbers.
Is there a workaround for this?