title: How to write a lambda function that is conditional on two variables (columns) in python
body: <p>I have a data set, df, with two variables, x and y. I want to write a function that does the following: </p>
  
  <blockquote>
    <p>x if x>100 and y&lt;50 else y</p>
  </blockquote>
  
  <p>I am used to doing data analysis in STATA so I'm relatively new to pandas for data analysis. If it helps, in stata it would look like: </p>
  
  <blockquote>
    <p>replace x = cond(x>100 &amp; y&lt;50, x, y) </p>
  </blockquote>
  
  <p>In other words, the function is conditional on two columns in df and will return a value from one variable or the other in each row depending on whether the condition is met.</p>
  
  <p>So far I have been creating new variables through new functions like: </p>
  
  <blockquote>
    <p>df.dummyVar = df.x.apply(lambda x: 1 if x>100 else 0) </p>
  </blockquote>
  
  <p>Using StackOverflow and the documentation I have only been able to find how to apply a function dependent on a single variable to more than one column (using the axis option). Please help. </p>