title: How to use nested if statements within a pandas dataframe - Python
body: <p>I have a dataframe that consists of the U.S. unemployment rate, a year long moving average for the unemployment rate, a monthly price of the S&amp;P 500 along with a 10 month moving average.</p>
  
  <pre><code>start = "1950-01-01"
  end = "2017-12-31"
  
  fred_unemployment = quandl.get("FRED/UNRATE", start_date=start, end_date=end)
  fred_unemployment["UE 12MMA"] = fred_unemployment["Value"].rolling(12).mean()
  fred_unemployment["SPY"] = spy["Adj Close"]
  fred_unemployment["SPY 200d MA"] = spy["Adj Close"].rolling(10).mean()
  </code></pre>
  
  <p>From this I'm looking to identify the rows where:
  fred_unemployment["Value"] > fred_unemployment["UE 12MMA"]</p>
  
  <p>Then have it check if fred_unemployment["SPY"] &lt; fred_unemployment["SPY 200d MA"]</p>
  
  <p>Ideally this would result in certain buy and sell decisions by using what I believe are called "nested if statements" ie:</p>
  
  <pre><code>if this:
      if that:
          Sell Stock
      else:
          Buy Stock
  else:
      Buy Stock
  </code></pre>
  
  <p>Here's where I'm stuck. I've created another column called fred_unemployment["Signals"] and am currently using np.where that sets the "Signal" columns values to either 1 or 0 depending on the conditions. </p>
  
  <pre><code>fred_unemployment["Signal"] = np.where((fred_unemployment["Value"] &gt; fred_unemployment["UE 12MMA"]) &amp;
                                     (fred_unemployment["SPY"] &lt; fred_unemployment["SPY 200d MA"]), 0, 1)
  </code></pre>
  
  <p>So while this works in some cases it wouldn't work in the case where the "Values" &lt; "UE 12MMA" and the "SPY" > "SPY 200d MA" because instead of the nested if statements it's using the if and statement. </p>
  
  <p>Here's a few lines from the dataframe</p>
  
  <p>In summary, how can I create the nested if statement result to sort through the pandas dataframe?</p>
  
  <p>Here's the head of the dataframe:</p>
  
  <pre><code>            Value  UE 12MMA     SPY  SPY 200d MA  Signal
  Date                                                    
  1990-01-01    5.4       NaN  359.69      334.658       1
  1990-02-01    5.3       NaN  328.79      337.898       1
  1990-03-01    5.2       NaN  332.74      340.260       1
  1990-04-01    5.4       NaN  338.70      341.933       1
  1990-05-01    5.4       NaN  332.25      343.235       1
  </code></pre>
  
  <p>the tail:</p>
  
  <pre><code>             Value UE 12MMA   SPY      SPY 200d MA   Signal
  Date                                                     
  2010-08-01    9.5  9.741667  1125.86     1109.393       1
  2010-09-01    9.5  9.716667  1080.29     1113.134       1
  2010-10-01    9.4  9.666667  1146.24     1116.872       1
  2010-11-01    9.8  9.658333  1184.38     1122.011       1
  2010-12-01    9.3  9.608333  1206.07     1133.699       1
  </code></pre>
  
  <p>and a few where you can see the signal as 0. </p>
  
  <pre><code>             Value  UE 12MMA  SPY      SPY 200d MA  Signal
  Date                                                     
  1990-12-01    6.3  5.616667   324.10      335.106       0
  1991-01-01    6.4  5.700000   326.45      334.477       0
  1991-12-01    7.3  6.850000   381.40      382.923       0
  1998-09-01    4.6  4.558333   994.26     1057.417       0
  2001-01-01    4.2  3.983333  1283.27     1430.739       0
  </code></pre>
  
  <p>Thanks!</p>