0

I have a dataframe and want to create a new column in the dataframe based on certain condition.

The condition goes like this: if (column_1 >=5 and column_2 >= 10) or (column_3 >=6) then 1 else 0.

What is the most efficient way to create this equation in pandas.

copying the note from the comments to the body of the question.

I have tries this:

df['Flag']= np.where((((df['Column_1']>=5) & (df['Column_2']>= 10)) | df['Column_3']>=6),1,0)

Thanks!

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • 2
    What have you tried? This is really just a one-liner. – Tim Roberts Jan 04 '22 at 23:30
  • Please [edit] your question to include a [mcve] with sample input data, _code_ for what you've already tried based on your own research, and a description of what went wrong with your attempt(s) – G. Anderson Jan 04 '22 at 23:31
  • I have tries this: df['Flag']= np.where((((df['Column_1']>=5) & (df['Column_2']>= 10)) | df['Column_3']>=6),1,0) – aditya m.honade Jan 04 '22 at 23:34
  • @adityam.honade please update your original post with what you tried, properly formatted as code, and explain why it doesn't match up with your expectations / desired outcome. – ddejohn Jan 04 '22 at 23:35
  • `df['Flag'] = df[(df['column_1'] >= 5 & df['column_2'] >= 10) | (df['column_3'] >= 6)].astype(int)` should do what you want. – ddejohn Jan 04 '22 at 23:38
  • Does this answer your question? [pandas create new column based on values from other columns / apply a function of multiple columns, row-wise](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns-apply-a-function-o) – itprorh66 Jan 05 '22 at 00:38

0 Answers0