Percent change of values that are not NaN

Advertisements

This is my dataframe:

df = pd.DataFrame({'a': [10, 11, 20, 80, 1, 22], 'b':['x', np.nan, 'x', np.nan, np.nan, 'x']})

And this is the output that I want:

    a    b     c
0  10    x     NaN
1  11  NaN     NaN
2  20    x     100
3  80  NaN     NaN
4   1  NaN     NaN
5  22    x     10

I want to create column c which is the perecent change of values of column a that are not NaN in b.
For example 100 in c is the result of percent change of 20 and 10.

I have tried to create a new dataframe by using df.loc[df.b.notna(), 'a'].values but I still cannot get the result that I want.

>Solution :

You can calculate the pct_change() after selecting the rows from a corresponds to the not null value from b.

df['c'] = df.loc[df['b'].eq('x'), 'a'].pct_change().mul(100)
# or
df['c'] = df.loc[df['b'].notnull(), 'a'].pct_change().mul(100)
print(df)

    a    b      c
0  10    x    NaN
1  11  NaN    NaN
2  20    x  100.0
3  80  NaN    NaN
4   1  NaN    NaN
5  22    x   10.0

Leave a ReplyCancel reply