[英]how to calculate percent change between columns in pandas?
I am new to Python and pandas. I created a dataframe and want to calculate the percent change between 2 columns.我是 Python 和 pandas 的新手。我创建了一个 dataframe 并想计算 2 列之间的百分比变化。 I know of the pct_change function in pandas but that works between rows.
我知道 pandas 中的 pct_change function 但行之间有效。
import pandas as pd
import matplotlib.pyplot as plt
UKnewcars = pd.DataFrame({
'Model': ["Diesel", "MHEV Diesel", "Petrol", "MHEV petrol","BEV", "PHEV", "HEV" ],
'2021': [10658,11448,94314,27326,32721,13884,24961],
'2020': [46996,13484,176532,30382,21903,12454,26290]})
I keep getting the error message below for any formula I try even a simple subtraction that I tried between these two columns.对于我尝试的任何公式,我不断收到下面的错误消息,甚至是我在这两列之间尝试的简单减法。
TypeError: unsupported operand type(s) for /: 'int' and 'str' TypeError: /: 'int' 和 'str' 不支持的操作数类型
These are the data types of the data frame这些是数据框的数据类型
Model object
2021 int64
2020 int64
dtype: object
If you know how I can calculate the percent change between these columns please let me know.如果您知道我如何计算这些列之间的百分比变化,请告诉我。 If it is a data type issue let me know as well.
如果是数据类型问题,也请告诉我。 I know pandas has the int64 data type and I assume it corresponds to the int data type in Python. I have included a screenshot of how the dataframe looks like on my screen, see below:
我知道 pandas 具有 int64 数据类型,我假设它对应于 Python 中的 int 数据类型。我在屏幕上包含了 dataframe 的屏幕截图,如下所示:
Convert string column to index
:将字符串列转换为
index
:
UKnewcars = UKnewcars.set_index('Model').pct_change(axis=1)
print (UKnewcars)
2021 2020
Model
Diesel NaN 3.409458
MHEV Diesel NaN 0.177848
Petrol NaN 0.871748
MHEV petrol NaN 0.111835
BEV NaN -0.330613
PHEV NaN -0.102996
HEV NaN 0.053243
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.