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 |
这篇关于任务,根据下一行添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:任务,根据下一行添加新列


基础教程推荐
猜你喜欢
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01