I have a pandas DataFrame:
df = pd.DataFrame({'A':['a', 'a', 'a', 'a', 'a'], 'B':[1, 2, 3, 4, 5]})
and a user-defined function:
def f(df):
return df.max() - df.min()
When I run the following code:
df.groupby('A').apply(f)
How many times the function f will be called? I tested this code, and found f will be called 4 times, could anybody explain this?
It should be called ngroup times , if you have two group in A , it will be called twice. Also , you can using np.ptp
df.groupby('A').B.apply(np.ptp)
Out[52]:
A
a 4
Name: B, dtype: int64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.