I would like to transform values from a dataframe to columns of their own within the same dataframe, so that each name entry has only one row (instead of the same entry appearing in multiple rows for different column entries). Below is an example.
This is the dataframe I have:
Name Food Grams
0 Tammy Fish 200
1 Tammy Rice 105
2 Wenny Rice 250
3 Wenny Eggs 100
4 Wenny Eggs 90
5 Steve Plums 10
6 Steve Eggs 90
(code for dataframe is below)
And I would like to transform this to the following:
Name Fish Rice Eggs Plums
0 Tammy 200 105 NaN NaN
1 Wenny NaN 250 190 NaN
2 Steve NaN NaN 90 10
(The NaN
s can also be 0
, both are fine).
Note that in situations where the same person eats the same food on different occasions, the grams of food consumed are added together within the same column (e.g. with Wenny and eggs).
Code for df
:
data = {'Name': ['Tammy', 'Tammy', 'Wenny', 'Wenny', 'Wenny',
'Steve', 'Steve'],
'Food': ['Fish', 'Rice', 'Rice', 'Eggs',
'Eggs', 'Plums', 'Eggs'],
'Grams': [200, 105, 250, 100, 90, 10, 90]}
df = pd.DataFrame(data, columns = ['Name', 'Food', 'Grams'])