Apply Functions By Group In Pandas

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

Preliminaries

# import pandas as pd
import pandas as pd

Create a simulated dataset

# Create an example dataframe
data = {'Platoon': ['A','A','A','A','A','A','B','B','B','B','B','C','C','C','C','C'],
       'Casualties': [1,4,5,7,5,5,6,1,4,5,6,7,4,6,4,6]}
df = pd.DataFrame(data)
df
Casualties Platoon
0 1 A
1 4 A
2 5 A
3 7 A
4 5 A
5 5 A
6 6 B
7 1 B
8 4 B
9 5 B
10 6 B
11 7 C
12 4 C
13 6 C
14 4 C
15 6 C

Apply A Function (Rolling Mean) To The DataFrame, By Group

# Group df by df.platoon, then apply a rolling mean lambda function to df.casualties
df.groupby('Platoon')['Casualties'].apply(lambda x:x.rolling(center=False,window=2).mean())
0     NaN
1     2.5
2     4.5
3     6.0
4     6.0
5     5.0
6     NaN
7     3.5
8     2.5
9     4.5
10    5.5
11    NaN
12    5.5
13    5.0
14    5.0
15    5.0
dtype: float64