简体   繁体   English

在 Python 中展示行之间的百分比增加/百分比变化

[英]Showcase the Percent Increase/Percent Change between rows in Python

I have a df that shows Dates and Values and I would like to calculate the percent change or percent increase between rows - showing the difference between the change from 10/07/2020 to 10/08/2020我有一个显示日期和值的 df,我想计算行之间的百分比变化或百分比增加 - 显示从 10/07/2020 到 10/08/2020 的变化之间的差异

   Date           Value
    
   10/08/2020     30
   10/07/2020     30
   10/06/2020     30
   10/05/2020     30
   10/04/2020     30

Desired Result想要的结果

   Date           Value     PercentIncrease 
    
   10/08/2020     30        0%
   10/07/2020     30        0%
   10/06/2020     30        0%
   10/05/2020     30        0%
   10/04/2020     30        0%

This is what I am doing:这就是我正在做的:

(df['Delta'] / df['Delta'].shift(1) - 1).fillna(0)

Is there a way to show the percent sign too?有没有办法也显示百分号?

如果你想要一个带有%的字符串:

df['PctIncrease'] = [f'{x:.2%}' for x in (df['Value'].div(df['Value'].shift(1)) - 1).fillna(0)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM