Awitz
Awitz

Reputation: 306

pandas if column is true, perform percent change on that row

I have a df with an additional column of boolean values based off a conditional statement.

df = pd.DataFrame({'col1': [1,2,3,2.5,5,2]})
df['bool'] =  df['col1'] >= 3

The df looks like...

    col bool
0   1.0 False
1   2.0 False
2   3.0 True
3   2.5 False
4   5.0 True
5   2.0 False

I would like to get pct_change() of "col1" if "bool" is true and if false return NaN. The output should look something like...

    col pct_change
0   1.0 NaN
1   2.0 NaN
2   3.0 -0.169
3   2.5 NaN
4   5.0 -0.400
5   2.0 NaN

What would be the best way of going about this?

Upvotes: 0

Views: 270

Answers (1)

Chris
Chris

Reputation: 29742

Use numpy.where to use df["bool"] as a boolean mask:

df["pct_change"] = np.where(df["bool"], df["col"].pct_change().shift(-1), np.nan)
print(df)

Output:

   col   bool  pct_change
0  1.0  False         NaN
1  2.0  False         NaN
2  3.0   True   -0.166667
3  2.5  False         NaN
4  5.0   True   -0.400000
5  3.0  False         NaN

Upvotes: 2

Related Questions