我正在尝试根据另一列的条件创建一个新列,其范围为该计数。但是,我得到了ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
我试图使用同一列两次以使范围,但它不起作用。问题出在哪儿?
df.loc[(df["count_words"] > 100 & df["count_words"] <= 300), "length"] = "keskipitkä"
df.loc[df["count_words"] <= 100, "lenght"] = "lyhyt"
df.loc[df["count_words"] > 300, "length"] = "pitkä"
问题出在()
,因为操作员的优先级:
df.loc[(df["count_words"] > 100) & (df["count_words"] <= 300), "length"] = "keskipitkä"
另一个想法是在这里使用cut
:
df=pd.DataFrame({'count_words':[10, 100, 200, 300, 4999]})
df["lenght"] = pd.cut(df["count_words"],
bins= [-np.inf, 100, 300, np.inf],
labels=['lyhyt','keskipitkä','pitkä'])
print (df)
count_words lenght
0 10 lyhyt
1 100 lyhyt
2 200 keskipitkä
3 300 keskipitkä
4 4999 pitkä
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句