非 NaN 值的百分比变化

huangapple go评论78阅读模式
英文:

Percent change of values that are not NaN

问题

这是我的数据框架:

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

这是我想要的输出:

    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

我想要创建一个名为c的列,该列是列a中不为NaN的值的百分比变化。例如,c中的100是20和10的百分比变化的结果。

我尝试创建一个新的数据框架,使用df.loc[df.b.notna(), 'a'].values,但我仍然无法得到我想要的结果。

英文:

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.

答案1

得分: 1

df['c'] = df.loc[df['b'].eq('x'), 'a'].pct_change().mul(100)
# 或
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
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 12:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定