1

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!

1
  • OR your conditions together with | to set the value if any of the conditions are True? Commented Oct 5, 2021 at 18:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.