Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two bins from 4 levels in pandas dataframe?

Tags:

python

pandas

One of my columns in pandas dataframe looks like following. I would like to bin my "Daughter" column such that row with 0 will receive label of "None" and rows containing 1,2,3,4 will receive labels of "Some".

Current Dataset that contains column

Daughter
0
0
1
2
4
3

Expected Output:

Daughter Daugther_fact
0         None
0         None
1         Some
2         Some
4         Some
3         Some

I new newbie to python. I know I have to use pd.cut to assign my labels, but I am not sure how to achieve this. Any help is appreciated!

like image 999
Data_is_Power Avatar asked Jan 31 '26 23:01

Data_is_Power


1 Answers

Is this what you need ?

pd.cut(df.Daughter,[-np.inf,0,np.inf],labels=['None','some'])
Out[35]: 
0    None
1    None
2    some
3    some
4    some
5    some
Name: Daughter, dtype: category
Categories (2, object): [None < some]
like image 159
BENY Avatar answered Feb 03 '26 13:02

BENY