簡體   English   中英

熊貓df-計算百分比差異不變

[英]pandas df - calculate percentage difference not change

我要計算百分比差異不變或只是兩個值之間的差異。

我的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%

diff除以diff用絕對值除以absrolling 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) + '%'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM