0

I have the following dataframe: enter image description here

enter image description here

how to calculate the average number of flights pr day (3.67 in this case as we have 3 days with the total number of flights of 11. What would be the command in pandas?

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Please, data as text only in your question, pictures make it _VERY_ hard for users to give you a good answer. – cs95 Jan 07 '18 at 17:44
  • Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine Jan 07 '18 at 19:28

1 Answers1

4

apply + groupby get one output

df.groupby('Orign').date.apply(lambda x : len(x)/x.nunique())
Out[331]: 
Orign
1    3.666667
Name: date, dtype: float64

transform + groupby assign to all

df.groupby('Orign').date.transform(lambda x : len(x)/x.nunique())
Out[332]: 
0     3.666667
1     3.666667
2     3.666667
3     3.666667
4     3.666667
5     3.666667
6     3.666667
7     3.666667
8     3.666667
9     3.666667
10    3.666667
Name: date, dtype: float64

Sample Data input

dict = {'Orign': [1,1,1,1,1,1,1,1,1,1,1], 'date': ['A', 'A', 'A', 'B','B','C','C','C','C','C','C']}
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ I just generated the sample data :-) append with dict – BENY Jan 07 '18 at 17:48
  • This gives the mean number of flights per day *per origin*, right? If OP wants to get the mean count of *total* flights per day (between all origins and to all destinations), I think this works: `1.0 * df.shape[0] / df.date.nunique()` – Peter Leimbigler Jan 07 '18 at 17:49
  • df.groupby('Orign').date.apply(lambda x : len(x)/x.nunique()) - it gives me 3 df.groupby('Orign').date.transform(lambda x : len(x)/x.nunique()) - it gives also 3 1.0 * df.shape[0] / df.date.nunique() - this gives 3.67 which is right answer; – Krzysztof Musialik Jan 07 '18 at 19:02