Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Percent change of values that are not NaN

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading