1

I have the following dataframe:

df = pd.DataFrame([{'animal': 'dog', 'years':10}, 
                   {'animal': 'dog', 'years':5},
                   {'animal': 'cat', 'years':3},
                   {'animal': 'cat', 'years':7}])

giving me:

  animal  years
0    dog     10
1    dog      5
2    cat      3
3    cat      7

Given the number of cats and dogs is always the same, how can I can turn it into:

       dog   cat
years  10     3
        5     7

My ultimate goal is to make a boxplot showing years distrubution across each animal type.

dzieciou
  • 4,049
  • 8
  • 41
  • 85
  • Does this answer your question? [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – villoro Mar 06 '20 at 11:59
  • @villoro Unfortunately not. It lists pivot methods in pandas that seem to require aggregation of results. I do not need aggregation. jezrael's answer answers my question perfectly and leads straight to the point without going into lengthy explanations. – dzieciou Mar 06 '20 at 12:46

1 Answers1

2

Use GroupBy.cumcount for counter per animal and reshape by DataFrame.set_index and Series.unstack:

df1 = (df.set_index([df.groupby('animal').cumcount(), 'animal'])['years']
         .unstack()
         .rename_axis(None, axis=1))
print (df1)
   cat  dog
0    3   10
1    7    5
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252