Dask, add new column based on next row(任务,根据下一行添加新列)
本文介绍了任务,根据下一行添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个DASK数据框,最后一列是这个问题的重要信息:
Dask DataFrame Structure:
asks[0].amount asks[1].amount asks[2].amount asks[3].amount asks[4].amount asks[5].amount asks[6].amount asks[7].amount asks[8].amount asks[9].amount asks[10].amount asks[11].amount asks[12].amount asks[13].amount asks[14].amount asks[15].amount asks[16].amount asks[17].amount asks[18].amount asks[19].amount asks[20].amount asks[21].amount asks[22].amount asks[23].amount asks[24].amount bids[0].amount bids[1].amount bids[2].amount bids[3].amount bids[4].amount bids[5].amount bids[6].amount bids[7].amount bids[8].amount bids[9].amount bids[10].amount bids[11].amount bids[12].amount bids[13].amount bids[14].amount bids[15].amount bids[16].amount bids[17].amount bids[18].amount bids[19].amount bids[20].amount bids[21].amount bids[22].amount bids[23].amount bids[24].amount currentPrice
npartitions=1
float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64 float64
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
现在,我需要根据下一行‘CurrentPrice’添加一个新的列(名为SuctPrice)。例如:
row1: ask......, bids....., currentPrice(11), succPrice(12)
row2: ask......, bids....., currentPrice(12), succPrice(17)
row3: ask......, bids....., currentPrice(17), succPrice(.....)
如何才能获得此结果?数据帧非常大,因此我需要使用Dask
推荐答案
使用shift(-1)
Dasksshift的功能与 pandas shift的功能相同。也就是说,如果要使用下一行的值,则必须使用shift(-1)。
请记住,数据帧的最后一个值将是nan。
代码示例
import dask
# Create data
df = (dask.datasets.timeseries()
.drop(columns=['id', 'name', 'y'])
.rename(columns={'x': 'currentPrice'}))
# Assign `succPrice` equal to the next `currentPrice`
df = df.assign(succPrice=df['currentPrice'].shift(-1))
df.tail()
| timestamp | currentPrice | succPrice |
|:--------------------|---------------:|------------:|
| 2000-01-30 23:59:55 | -0.241575 | 0.65083 |
| 2000-01-30 23:59:56 | 0.65083 | 0.742577 |
| 2000-01-30 23:59:57 | 0.742577 | 0.313805 |
| 2000-01-30 23:59:58 | 0.313805 | 0.556262 |
| 2000-01-30 23:59:59 | 0.556262 | nan |
这篇关于任务,根据下一行添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:任务,根据下一行添加新列
基础教程推荐
猜你喜欢
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
