英文:
Convert pandas column values based on groupings of values
问题
我有一个带有值1.0
,2.0
,3.0
,4.0
和5.0
的pandas列,如下所示:
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
我想将值为1.0或2.0的行全部赋值为1.0,将3.0和4.0变为2.0,将5.0变为3.0。根据这些分组,我该如何重新分配这些值。我最初考虑使用np.where()
,但现在不确定如何用np.where()
逻辑实现,因为那似乎更适合转换为二进制变量。也许只是使用.loc()
进行掩码处理?
谢谢。
英文:
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.
答案1
得分: 1
根据你的模式,进行简单的算术运算:加1,然后进行整除以2:
df['new'] = df['col'].add(1).floordiv(2)
或者使用 cut
:
df['new'] = pd.cut(df['col'], [0, 2, 4, 6], labels=[1, 2, 3])
请注意,cut
会给你一个分类类型。
示例:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论