ZacAttack ZacAttack - 2 months ago 6
Python Question

How do I get np.where to accept more arguments, so it will filter >, < , and =; not just > and <?

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).

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.

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"

This is the current output when I print df

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 . . .


This is my 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 > 0 , 'Up','Down');df

print(df)

Answer

Why is it displaying "Down" in the U_D_F column when the number in the PofChg column is "Zero"

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

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"

np.where(df.PofChg > 0 , 'Up', np.where(df.PofChg == 0, 'Flat', 'Down'))

If df.PofChg > 0, it chooses 'Up'; otherwise, if df.PofChg == 0, it choose 'Flat', and otherwise 'Down'.