Python 如何从熊猫数据框中的当前行中减去前一行并将其应用于每一行;不使用循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39479919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:19:13  来源:igfitidea点击:

How do I subtract the previous row from the current row in a pandas dataframe and apply it to every row; without using a loop?

pythonpandasnumpydataframeindexing

提问by ZacAttack

I am using Python3.5 and I am working with pandas. I have loaded stock data from yahoo finance and have saved the files to csv. My DataFrames load this data from the csv. This is a copy of the ten rows of the csv file that is my DataFrame

我正在使用 Python3.5,并且正在使用 Pandas。我已经从雅虎财经加载了股票数据并将文件保存到 csv。我的 DataFrames 从 csv 加载这些数据。这是作为我的 DataFrame 的 csv 文件的十行的副本

  Date       Open       High      Low     Close    Volume   Adj Close  
1990-04-12  26.875000  26.875000  26.625  26.625      6100  250.576036
1990-04-16  26.500000  26.750000  26.375  26.750       500  251.752449
1990-04-17  26.750000  26.875000  26.750  26.875      2300  252.928863
1990-04-18  26.875000  26.875000  26.500  26.625      3500  250.576036
1990-04-19  26.500000  26.750000  26.500  26.750       700  251.752449
1990-04-20  26.750000  26.875000  26.750  26.875      2100  252.928863
1990-04-23  26.875000  26.875000  26.750  26.875       700  252.928863
1990-04-24  27.000000  27.000000  26.000  26.000      2400  244.693970
1990-04-25  25.250000  25.250000  24.875  25.125      9300  236.459076
1990-04-26  25.000000  25.250000  24.750  25.000      1200  235.282663

I know that I can use iloc, loc, ix but these values that I index will only give my specific rows and columns and will not perform the operation on every row. For example: Row one of the data in the open column has a value of 26.875 and the row below it has 26.50. The price dropped .375 cents. I want to be able to capture the % of Increase or Decrease from the previous day so to finish this example .375 divided by 26.875 = 1.4% decrease from one day to the next. I want to be able to run this calculation on every row so I know how much it has increased or decreased from the previous day. The index functions I have tried but they are absolute, and I don't want to use a loop. Is there a way I can do this with the ix, iloc, loc or another function?

我知道我可以使用 iloc、loc、ix,但是我索引的这些值只会给出我的特定行和列,并且不会对每一行执行操作。例如:打开列中数据的第一行值为 26.875,其下方的行值为 26.50。价格下跌了 0.375 美分。我希望能够捕获前一天的增加或减少百分比,以便完成此示例,0.375 除以 26.875 = 1.4% 从一天到下一天减少。我希望能够在每一行上运行这个计算,这样我就知道它比前一天增加了多少或减少了多少。我尝试过的索引函数但它们是绝对的,我不想使用循环。有没有办法用 ix、iloc、loc 或其他函数来做到这一点?

回答by MaxU

you can use pct_change()or/and diff()methods

您可以使用pct_change()或/和diff()方法

Demo:

演示:

In [138]: df.Close.pct_change() * 100
Out[138]:
0         NaN
1    0.469484
2    0.467290
3   -0.930233
4    0.469484
5    0.467290
6    0.000000
7   -3.255814
8   -3.365385
9   -0.497512
Name: Close, dtype: float64

In [139]: df.Close.diff()
Out[139]:
0      NaN
1    0.125
2    0.125
3   -0.250
4    0.125
5    0.125
6    0.000
7   -0.875
8   -0.875
9   -0.125
Name: Close, dtype: float64

回答by vozman

MaxU solutions suits in your case. If you want to perform more complex computations based on your previous rows you should use shift

MaxU 解决方案适合您的情况。如果您想根据之前的行执行更复杂的计算,您应该使用shift