How to apply an if between function on a column in pandas' DataFrame

Be2

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!

MNM

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.

edited at
0

Comments

0 comments
Login to comment

Related

pandas DataFrame, how to apply function to a specific column?

How to apply a function to each row of one column in a pandas dataframe?

How to speed up Pandas apply function to create a new column in the dataframe?

How to apply a user-defined function to a column in pandas dataframe?

How to apply a function to a DataFrame column?

Pandas Dataframe Multiple column headers, apply function

Apply a function to every column of a dataframe in pandas

Apply function on each column in a pandas dataframe

Apply function to single column of pandas Dataframe

Apply a custom function to each column of a pandas dataframe

Pandas dataframe apply function to entire column

How to apply stemming to a column in a pandas dataframe

How to apply Lemmatization to a column in a pandas dataframe

How to apply LabelEncoder for a specific column in Pandas dataframe

Pandas:How to apply a complex function to a column of a dataframe, with two other columns as the input of the function?

How to apply a function to a column(text) in a dataframe(R)?

How to apply lambda function to column of dataframe correctly?

How to apply function on the basis of column condition in a dataframe

How to apply a function to a column of a Spark DataFrame?

How do you apply a function to a dataframe column

How to apply a function to a dataframe column in R?

How to apply my function to DataFrame column?

how to apply lme function to each column of dataframe?

Pandas dataframe apply function to column strings based on other column value

pandas - apply a function on column, using a second dataframe as an input for the function?

How to apply by chunks a function to pandas dataframe?

How to apply a function against a dataframe in pandas?

How to apply a function with multiple conditions to pandas dataframe?

How to apply a function inside a class into a pandas Dataframe