Applying pandas qcut bins to new data

You can do it by passing retbins=True.

Consider the following DataFrame:

import pandas as pd
import numpy as np
prng = np.random.RandomState(0)
df = pd.DataFrame(prng.randn(100, 2), columns = ["A", "B"])

pd.qcut(df["A"], 20, retbins=True, labels=False) returns a tuple whose second element is the bins. So you can do:

ser, bins = pd.qcut(df["A"], 20, retbins=True, labels=False)

ser is the categorical series and bins are the break points. Now you can pass bins to pd.cut to apply the same grouping to the other column:

pd.cut(df["B"], bins=bins, labels=False, include_lowest=True)
Out[38]: 
0     13
1     19
2      3
3      9
4     13
5     17
...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)