# How to map numeric data into categories / bins in Pandas dataframe?

Learn, how to map numeric data into categories / bins in Pandas dataframe? By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

## Problem statement

Suppose we are given the dataframe representing the marks and grades of a student in different subjects. There are three types of grades which depend on the marks. Suppose the marks are greater than 80 then the grade is 'A'.

## Mapping numeric data into categories / bins in Pandas dataframe

So, we need to map these data into categories so that the result will tell us which grade lies in which range of marks.

For this purpose, we will use they vectorize method of numbers. Let us understand with the help of an example,

## Python program to map numeric data into categories / bins in Pandas dataframe

```# Importing pandas
import pandas as pd

# Import numpy
import numpy as np

# Creating a dictionary
d = {
'Marks':[90,83,78,76,54,60],
}

# Creating a dataframe
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Creating bins
bins = [50, 60, 70, 80, 90]

# Creating a list of strings
# which shows the range of marks
range = ['50-60','60-70','70-80','80-90','90-100']

# Create a dictionary of bins and range
d = dict(enumerate(range, 1))

df['Marks-Range'] = np.vectorize(d.get)(np.digitize(df['Marks'], bins))

# Display result
print("Result:\n",df,"\n")
```

### Output

The output of the above program is: