英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论