I am trying to change a numeric column into a categorical one by applying if the value is in range 4 to 8 then "yes" else "No" what is the simplest way to do that???? Thank you in advance!
Try something like this
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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments