Skip to content Skip to sidebar Skip to footer

Pandas: Group By Equal Range

This is an example of my data frame: df_lst = [ {'wordcount': 100, 'Stats': 198765, 'id': 34}, {'wordcount': 99, 'Stats': 98765, 'id': 35}, {'wordcount': 200, 'Stats':

Solution 1:

use pd.cut() method:

In [92]: bins = np.arange(0, df['wordcount'].max().round(-2) + 100, 100)

In [94]: df.groupby(pd.cut(df['wordcount'], bins=bins, labels=bins[1:]))['Stats'].mean()
Out[94]:
wordcount
100    148765.0
200     18765.0
300    788765.0
400     12765.0
500    120349.5
600         NaN
700         NaN
800         NaN
900     10916.0
Name: Stats, dtype: float64

Solution 2:

import math
def roundup(x):
    return int(math.ceil(x / 100.0)) * 100
df['roundup']=df.wordcount.apply(roundup)
df.groupby('roundup').Stats.mean()
Out[824]: 
roundup
100    148765.0
200     18765.0
300    788765.0
400     12765.0
500    120349.5
900     10916.0
Name: Stats, dtype: float64

Post a Comment for "Pandas: Group By Equal Range"