数学在pandas dataframe列中的行上

问题描述 投票:0回答:1

我试图找到允许我从同一列中第二个值到最后一个值中减去列的最后一行中的值的代码。这是我尝试过的。

df_stock2['eps_median_est'][-1] - df_stock2['eps_median_est'][-2]

错误是:TypeError:需要一个整数

我的数据类型是

eps_median_est, dtype: float64
python python-3.x pandas indexing series
1个回答
1
投票

您可以使用iat(或iloc)通过整数位置进行快速标量访问:

res = df_stock2['eps_median_est'].iat[-1] - df_stock2['eps_median_est'].iat[-2]

或者,使用NumPy:

A = df_stock2['eps_median_est'].values
res = A[-1] - A[-2]
© www.soinside.com 2019 - 2024. All rights reserved.