我正在尝试通过将数值列更改为类别列来应用(如果该值在4到8之间),然后是“是”,否则是“否” 最简单的方法是什么? 预先谢谢你!
答案 0 :(得分:0)
尝试这样的事情
from pandas import DataFrame
Numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
df = DataFrame(Numbers,columns=['set_of_numbers'])
df.loc[4 <= df.set_of_numbers <= 8, 'equal_or_lower_than_4?'] = 'True'
df.loc[df.set_of_numbers < 4, 'equal_or_lower_than_4?'] = 'False'
df.loc[df.set_of_numbers > 8, 'equal_or_lower_than_4?'] = 'False'
print (df)
答案 1 :(得分:0)
Python的numpy
模块提供了一个根据条件(即条件)选择元素的功能。
numpy.where(condition[, x, y])
参数: condition :一个条件表达式,返回布尔型的Numpy数组 x,y:数组(可选,即通过或不通过)
示例:
s = pd.Series(range(5))
s.where(s > 1, 10)
输出:
0 10
1 10
2 2
3 3
4 4