简体   繁体   English

带有条件数据的数据框列-Python

[英]Data frame column with conditional data - Python

I have some percentages in a data frame column 我在数据框列中有一些百分比

pc
0.32
0.45
0.49
0.60
0.68
0.87

And i want to end up with something like this 我想结束这样的事情

pc     group
0.32    1
0.45    2
0.49    2
0.60    2
0.68    3
0.87    3

I've tried 我试过了

df["group"]=3

if df["pc"]<0.66:
    df["group"]=2

elif df["pc"]<0.33:
    df["group"]=1

but all i get is 但我得到的只是

ValueError: The truth value of an array with more than one element is ambiguous.

Any ideas? 有任何想法吗?

df["group"][df["pc"] < 0.66] = 2
df["group"][df["pc"] < 0.33] = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM