我有一个数据框(df):
Share Price Count Cat
0.400888 679 473 1
0.000000 245 194 1
0.200000 100 23 1
0.050000 87 8 1
0.400000 425 15 1
0.500000 858 7 1
我需要根据以下条件将每个ob分配到一个类别(Cat
):
给出价格限制(lim=100
)
If Price <= lim then Cat=1
If Price > lim & Share=0 then Cat=2
If Price > lim & Share>0 then Cat=3
因此,在此示例中,我将获得以下输出:
Share Price Count Cat
0.400888 679 473 3
0.000000 245 194 2
0.200000 100 23 1
0.050000 87 8 1
0.400000 425 15 3
0.500000 858 7 3
我尝试了以下两种解决方案,但均无效果:
def categorize(x):
if x['Price'] > lim and x['Share'] > 0: return 3
elif x['Price'] > lim and x['Share'] == 0: return 2
else: return 1
df.apply(categorize)
这:
df['Cat'][df['Price']>lim]=2 #SettingWithCopyWarning but it works
df['Cat'][df['Price']>lim & df['Share']>0 ]=3 # TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]