Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the left edge as the label of pandas.cut?

This is my DataFrame:

import pandas as pd
df = pd.DataFrame(
    {
        'high': [110, 110, 101, 101, 115, 300],
    }
)

And this is the expected output. Column bin should be created:

   high  bin
0   110  105.0
1   110  105.0
2   101  100.0
3   101  100.0
4   115  111.0
5   300  220.0

Basically bin is created by using pd.cut:

import numpy as np    
evaluation_bins = [100, 105, 111, 120, 220, np.inf]    
df['bin'] = pd.cut(df['high'], bins=evaluation_bins, include_lowest=True, right=False)

This gives me the category itself but I want the left edge as the output.

Honestly there was not much I could try to do. I could get the dtypes of df by df.dtypes but I don't know how to continue.

like image 581
Amir Avatar asked Oct 31 '25 03:10

Amir


2 Answers

Pass evaluation_bins to labels inside pd.cut, without the final edge (np.inf):

df['bin'] = pd.cut(df['high'], bins=evaluation_bins, labels=evaluation_bins[:-1], 
                   include_lowest=True, right=False)

Output:

   high  bin
0   110  105
1   110  105
2   101  100
3   101  100
4   115  111
5   300  220
like image 100
ouroboros1 Avatar answered Nov 01 '25 17:11

ouroboros1


you can modify the pd.cut function by setting include_lowest=True and right=False then use apply(lambda x: x.left)

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        'high': [110, 110, 101, 101, 115, 300],
    }
)

evaluation_bins = [100, 105, 111, 120, 220, np.inf]
df['bin'] = pd.cut(df['high'], bins=evaluation_bins, include_lowest=True, right=False)
df['bin'] = df['bin'].apply(lambda x: x.left)

print(df)

output:

   high    bin
0   110  105.0
1   110  105.0
2   101  100.0
3   101  100.0
4   115  111.0
5   300  220.0
like image 20
Sajjad Ranjbar Avatar answered Nov 01 '25 18:11

Sajjad Ranjbar