How do I get np.where to accept more arguments, so it will filter >, < , and =; not just > and <?
<p>I am using python 3.5 and have the numpy and pandas libraries imported. I have created a DataFrame called df, which has an index starting at zero, and two columns; Percentage of Change (PofChg) and Up, Down, or Flat (U_D_F).</p>

<p>For the U_D_F column I want to populate it with the words 'Up', 'Down', 'Flat', based off of the PofChg column. Up means it was greater than zero, Down means it was less than zero, and Flat means it was equal to zero.  </p>

<p>The np.where function seems to work well except for two things, 
(1) Why is it displaying "Down" in the U_D_F column when the number in the PofChg column is "Zero"
(2) How do I make the np.where function accept more arguments, i.e. instead of saying - if df.PofChg is > 0 , if true display "Up" or if false display "Down", I want to change it to - if df.PofChg is > 0, if true display "Up" or if false display "Down", but if it is equal to zero then Display "Flat"</p>

<p>This is the current output when I print df</p>

<pre><code>   PofChg U_D_F
0      -1  Down
1       0  Down
2       1    Up
3      -2  Down
4       0  Down
5       5    Up
6       3    Up
7      -6  Down
Press any key to continue . . .
</code></pre>

<p>This is my code</p>

<pre><code>import pandas as pd
import numpy as np


df = pd.DataFrame({'PofChg':[-1,0,1,-2,0,5,3,-6]})
df['U_D_F'] = np.where(df.PofChg &gt; 0 , 'Up','Down');df

print(df)
</code></pre>

<blockquote>
  <p>Why is it displaying "Down" in the U_D_F column when the number in the PofChg column is "Zero" </p>
</blockquote>

<p>That is because your condition to <code>np.where</code> was > 0, so, if it is 0, the condition fails, and it chooses the alternative.</p>

<blockquote>
  <p>I want to change it to - if df.PofChg is > 0, if true display "Up" or if false display "Down", but if it is equal to zero then Display "Flat"</p>
</blockquote>

<pre><code>np.where(df.PofChg &gt; 0 , 'Up', np.where(df.PofChg == 0, 'Flat', 'Down'))
</code></pre>

<p>If <code>df.PofChg &gt; 0</code>, it chooses <code>'Up'</code>; otherwise, if <code>df.PofChg == 0</code>, it choose <code>'Flat'</code>, and otherwise <code>'Down'</code>.</p>

python pandas numpy dataframe where