简体   繁体   English

如何在 Python 中找到日期时间值的百分比增加/减少?

[英]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.我需要打印从月/年到月/年的价值增加百分比以及从月/年到月/年的价值减少百分比。

dataframe format picture dataframe格式图片

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.

相关问题 如何使用 python 计算值列表中的百分比增加或减少 - How do i calculate the percentage increase or decrease in a list of values with python 无论日期顺序如何(在 Python 中),查找增加百分比和差异 - Find percent increase and diff regardless of the date order (in Python) 在Python / Pandas中找到Dataframe组的另一列的增减 - Find the increase, decrease in column of Dataframe group by an other column in Python / Pandas Pandas 行从真/假切换时的百分比增加/减少 - Pandas Percent increase/decrease when row switch from True/False 如何在没有if语句的情况下在python中计算百分比增加/减少乘数 - How to compute a percentage increase/decrease multiplier in python without if statements 如何在Python中计算时间序列的均值和最大值增加,减少 - How to calculate mean and max increase, decrease of time series in python 如何 python opencv 大小增加和减少 window 用于实时凸轮 - how to python opencv size increase and decrease window for a live cam Python - 如何减少大 O 并提高多个嵌套 for 循环的效率? - Python - How to decrease big O and increase efficiency in multiple nested for loops? 排序:x值增加,y值减少 - Sorting: x values increase, y values decrease 找到一列熊猫数据框的增加,减少 - Find the increase, decrease in a column of pandas Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM