计算百分比差异不改变

2024-03-28 19:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想计算不改变的百分比差异,或者只是两个值之间的差异。你知道吗

我的df:

                           Radisson Collection      
6                                                                  
Total awareness             0.440553            
Very/Somewhat familiar      0.462577           
Consideration               0.494652             
Ever used                   0.484620           

预期产量:

                            Radisson Collection      
6                                                                  
Total awareness             none            
Very/Somewhat familiar      4.87726%           
Consideration               6.70163%            
Ever used                   2.04886%          

计算如下:

Difference of 0.440553 and 0.462577 = |0.440553 - 0.462577|/((0.440553 + 0.462577)/2) = 0.022024/0.451565 = 0.048772601950993 = 4.8772601950993%

Tags: nonedf差异collectionusedverytotal百分比
1条回答
网友
1楼 · 发布于 2024-03-28 19:17:17

将差值除以^{}和绝对值,再除以^{}^{}mean

s = df['Radisson Collection'].rolling(2).mean()
df['new'] = df['Radisson Collection'].diff().abs().div(s) * 100
print (df)
                        Radisson Collection       new
Total awareness                    0.440553       NaN
Very/Somewhat familiar             0.462577  4.877260
Consideration                      0.494652  6.701636
Ever used                          0.484620  2.048869

如果需要百分比:

df['new'] = (df['Radisson Collection'].diff().abs().div(s) * 100)
                    .iloc[1:].round(5).astype(str) + '%'

相关问题 更多 >