DATA MUNGING
DATA CLEANING PYTHON
MACHINE LEARNING RECIPES
PANDAS CHEATSHEET
ALL TAGS
# How to apply functions in a Group in a Pandas DataFrame?

This recipe helps you apply functions in a Group in a Pandas DataFrame

Have you tried to apply a function on any dataset. One of the easiest way is to use apply function.

So this is the recipe on how we can apply functions in a Group in a Pandas DataFrame.

```
import pandas as pd
```

We have imported pandas which will be needed for the dataset.

We have made a dataframe by using a dictionary. We have passed a dictionary with different values to create a dataframe.
```
data = {"EmployeeGroup": ["A","A","A","A","A","A","B","B","B","B","B","C","C","C","C","C"],
"Points": [10,40,50,70,50,50,60,10,40,50,60,70,40,60,40,60]}
df = pd.DataFrame(data)
print("
The Original DataFrame")
print(df)
```

We have used apply function to find Rolling Mean, Average, Sum, Maximum and Minimum. For this we have used lambda function on each and every values of the feature.
```
print("
Rolling Mean:"); print(df.groupby("EmployeeGroup")["Points"].apply(lambda x:x.rolling(center=False,window=2).mean()))
print("
Average:"); print(df.groupby("EmployeeGroup")["Points"].apply(lambda x:x.mean()))
print("
Sum:"); print(df.groupby("EmployeeGroup")["Points"].apply(lambda x:x.sum()))
print("
Maximum:"); print(df.groupby("EmployeeGroup")["Points"].apply(lambda x:x.max()))
print("
Minimum:"); print(df.groupby("EmployeeGroup")["Points"].apply(lambda x:x.min()))
```

So the output comes as:

The Original DataFrame EmployeeGroup Points 0 A 10 1 A 40 2 A 50 3 A 70 4 A 50 5 A 50 6 B 60 7 B 10 8 B 40 9 B 50 10 B 60 11 C 70 12 C 40 13 C 60 14 C 40 15 C 60 Rolling Mean: 0 NaN 1 25.0 2 45.0 3 60.0 4 60.0 5 50.0 6 NaN 7 35.0 8 25.0 9 45.0 10 55.0 11 NaN 12 55.0 13 50.0 14 50.0 15 50.0 Name: Points, dtype: float64 Average: EmployeeGroup A 45.0 B 44.0 C 54.0 Name: Points, dtype: float64 Sum: EmployeeGroup A 270 B 220 C 270 Name: Points, dtype: int64 Maximum: EmployeeGroup A 70 B 60 C 70 Name: Points, dtype: int64 Minimum: EmployeeGroup A 10 B 10 C 40 Name: Points, dtype: int64

**
Download Materials
**

Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

In this time series project, you will explore various time series smoothing techniques and build a moving average time series forecasting model in python from scratch.

In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

In this time series project, you will build a model to predict the stock prices and identify the best time series forecasting model that gives reliable and authentic results for decision making.

In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

The project will use rasa NLU for the Intent classifier, spacy for entity tagging, and mongo dB as the DB. The project will incorporate slot filling and context management and will be supporting the following intent and entities. Intents : product_info | ask_price|cancel_order Entities : product_name|location|order id The project will demonstrate how to generate data on the fly, annotate using framework and how to process those for different pieces of training as discussed above .

Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.