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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With