Krzysztof Musialik
Krzysztof Musialik

Reputation: 1

calculating the average frequency of a given value in pandas

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?

Upvotes: 0

Views: 510

Answers (1)

BENY
BENY

Reputation: 323326

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']}

Upvotes: 4

Related Questions