Convert pandas column values based on groupings of values
Question:
I have a pandas columns with values 1.0
, 2.0
, 3.0
, 4.0
, and 5.0
like below:
0 5.0
1 2.0
2 3.0
3 3.0
4 5.0
...
1039 5.0
1040 1.0
1041 2.0
1042 4.0
1043 1.0
I want rows with values 1.0 or 2.0 to all have a value of 1.0, 3.0 and 4.0 to become 2.0, and 5.0 to become 3.0. How could I re-assign the values based on these groupings. I was thinking np.where()
at first but now I’m not sure how to implement that with np.where()
logic because that seems like it would be better suited for conversion to a binary variable. Maybe just masking with .loc()
?
Thanks.
Answers:
Given your pattern, use simple arithmetics: add 1, get the floor division by 2:
df['new'] = df['col'].add(1).floordiv(2)
Or use cut
:
df['new'] = pd.cut(df['col'], [0, 2, 4, 6], labels=[1, 2, 3])
Note that cut
will give you a categorical type.
Example:
col new
0 5.0 3.0
1 2.0 1.0
2 3.0 2.0
3 3.0 2.0
4 5.0 3.0
1039 5.0 3.0
1040 1.0 1.0
1041 2.0 1.0
1042 4.0 2.0
1043 1.0 1.0
I have a pandas columns with values 1.0
, 2.0
, 3.0
, 4.0
, and 5.0
like below:
0 5.0
1 2.0
2 3.0
3 3.0
4 5.0
...
1039 5.0
1040 1.0
1041 2.0
1042 4.0
1043 1.0
I want rows with values 1.0 or 2.0 to all have a value of 1.0, 3.0 and 4.0 to become 2.0, and 5.0 to become 3.0. How could I re-assign the values based on these groupings. I was thinking np.where()
at first but now I’m not sure how to implement that with np.where()
logic because that seems like it would be better suited for conversion to a binary variable. Maybe just masking with .loc()
?
Thanks.
Given your pattern, use simple arithmetics: add 1, get the floor division by 2:
df['new'] = df['col'].add(1).floordiv(2)
Or use cut
:
df['new'] = pd.cut(df['col'], [0, 2, 4, 6], labels=[1, 2, 3])
Note that cut
will give you a categorical type.
Example:
col new
0 5.0 3.0
1 2.0 1.0
2 3.0 2.0
3 3.0 2.0
4 5.0 3.0
1039 5.0 3.0
1040 1.0 1.0
1041 2.0 1.0
1042 4.0 2.0
1043 1.0 1.0