Home > Mobile >  How to set multiple conditions for a Dataframe while modifying the values?
How to set multiple conditions for a Dataframe while modifying the values?

Time:10-06

So, I'm looking for an efficient way to set up values within an existing column and setting values for a new column based on some conditions. If I have 10 conditions in a big data set, do I have to write 10 lines? Or can I combine them somehow...haven't figured it out yet. Can you guys suggest something?

For example:

data_frame.loc[data_frame.col1 > 50 ,["col1","new_col"]] = "Cool"

data_frame.loc[data_frame.col2 < 100 ,["col1","new_col"]] = "Cool"

Can it be written in a single expression? "&" or "and" don't work...

Thanks!

CodePudding user response:

yes you can do it, here is an example:

data_frame.loc[(data_frame["col1"]>100) & (data_frame["col2"]<10000) | (data_frame["col3"]<500),"test"] = 0

explanation:

the filter I used is (with "and" and "or" conditions): (data_frame["col1"]>100) & (data_frame["col2"]<10000) | (data_frame["col3"]<500)

the column that will be changed is "test" and the value will be 0

CodePudding user response:

You can try:

all_conditions = [condition_1, condition_2]
fill_with = [fill_condition_1_with, fill_condition_2_with]
df[["col1","new_col"]] = np.select(all_conditions, fill_with, default=default_value_here)
  • Related