[英]How to find percent increase/decrease with datetime values in Python?
I have attached a photo of how the data is formatted when I print the df in Jupyter, please check that for reference.我附上了一张我在 Jupyter 中打印 df 时如何格式化数据的照片,请检查以供参考。 Set the DATE column as the index, checked the data type of the index, and converted the index to be a datetime index.将 DATE 列设置为索引,检查索引的数据类型,并将索引转换为日期时间索引。
import pandas as pd
df = pd.read_csv ('UMTMVS.csv',index_col='DATE',parse_dates=True)
df.index = pd.to_datetime(df.index)
I need to print out percent increase in value from Month/Year to Month/Year and percent decrease in value from Month/Year to Month/Year.我需要打印从月/年到月/年的价值增加百分比以及从月/年到月/年的价值减少百分比。
The first correction pertains to how to read your DataFrame.第一个更正与如何读取 DataFrame 有关。
Passing parse_dates you should define a list of columns to be parsed as dates.传递parse_dates您应该定义要解析为日期的列列表。 So this instruction should be changed to:所以这条指令应该改为:
df = pd.read_csv('UMTMVS.csv', index_col='DATE', parse_dates=['DATE'])
and then the second instruction in not needed.然后不需要第二条指令。
To find the percent change in UMTMVS column, use: df.UMTMVS.pct_change()
.要查找UMTMVS列中的百分比变化,请使用: df.UMTMVS.pct_change()
。 For your data the result is:对于您的数据,结果是:
DATE
1992-01-01 NaN
1992-02-01 0.110968
1992-03-01 0.073036
1992-04-01 -0.040080
1992-05-01 0.014875
1992-06-01 -0.330455
1992-07-01 0.368293
1992-08-01 0.078386
1992-09-01 0.082884
1992-10-01 -0.030528
1992-11-01 -0.027791
Name: UMTMVS, dtype: float64
Maybe you should multiply it by 100 , to get true percents.也许你应该将它乘以100 ,以获得真实的百分比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.