data.pct_change().dropna()
时间: 2024-05-25 19:16:43 浏览: 15
This code calculates the percentage change between each element in a pandas DataFrame or Series with the previous element, and then drops the first row (which will be NaN as there is no previous element to compare it to). The result is a DataFrame or Series with the same shape as the original, but with each value replaced by the percentage change from the previous value.
For example, if we have a DataFrame 'data' with the following values:
| | A | B | C |
|---|---|---|---|
| 0 | 1 | 5 | 9 |
| 1 | 2 | 6 | 10 |
| 2 | 3 | 7 | 11 |
Then running `data.pct_change().dropna()` will give us:
| | A | B | C |
|---|----|---|----|
| 1 | 1.0 | 0.2 | 0.111111 |
| 2 | 0.5 | 0.166667 | 0.1 |
This shows the percentage change between each row and the previous row, with the first row dropped as there is no previous row to compare it to. For example, in column A the percentage change between row 1 and row 0 is 100% (as the value has doubled), and the percentage change between row 2 and row 1 is 50% (as the value has increased by half).
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)