0

I have a dataframe which columns look like actor | object | group | phone | day, i want to obtain a vector (or other name, i'm new to python) for each actor that counts how many times that actor appeared by day. The vector should be just the count by day.

The initial data in in a pandas dataframe and looks like this:

    actor  object  group  phone    Day
    john    don     saf   9234...  27-06-2015

Something like this: John (2,0,8,2,2,3,0,0,2,1,5,3,3,0,0...) I want every day to appear even if that actor doesn´t appear in that day, the count should be 0 in that case. I've tried many ways but never manage to get this.

The output intended is:

    Actor (data day 1) (data day 2) (data day 3) (data day 4)
    john      2             2           0            3        ...

or

    john (2,2,0,3,...)

the important ideia is to get for each actor the ammount of times ir appears in the inicial data for each day.

Mariana
  • 73
  • 8
  • 1
    Hi. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Dec 07 '18 at 11:44
  • That works for the count part, my problem is getting the each day to appear, a "column" or "space" for each day. That way every actor is counted how many times it appears in a day, even if its zero. – Mariana Dec 07 '18 at 12:21
  • I don't understand a lot about stackoverflow, do you mean like that? It loses the format whn i try do copy – Mariana Dec 07 '18 at 12:41
  • 1
    So need `df = pd.crosstab(df['actor'], df['Day'])` ? – jezrael Dec 07 '18 at 12:41
  • 1
    now data are better. – jezrael Dec 07 '18 at 12:41
  • yes! thank you, it worked – Mariana Dec 07 '18 at 12:45

1 Answers1

0

This should do what you want:

df.groupby(['actor', 'day']).agg({'day':'count'})
yatu
  • 86,083
  • 12
  • 84
  • 139