如何在python中为dataframe添加另一个labellike列?

2024-09-26 17:46:00 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有这样一个数据帧:

id      openPrice       closePrice
1         10.0             13.0
2         20.0             15.0   

我想添加另一个名为“移动”的列: 如果openprice<;close price设置为1,则else设置为-1

输出应为:

id      openPrice       closePrice    movement
1         10.0             13.0          1
2         20.0             15.0         -1

我可以在for循环中完成,但是对于一个有10000000行以上的df来说,这会很耗时。你知道吗

我是python新手,不知道有没有python函数可以有效地实现这一点。你知道吗

谢谢


Tags: 数据函数ltiddfforcloseprice
2条回答

pandas中实现快速性能的关键是使用向量化操作,即避免(正如您所注意的)缓慢的Python循环的内置操作。你知道吗

我比较喜欢的方法是对这样的更改进行标记,即对差异调用np.sign(当然,首先要做import numpy as np):

>>> df
   id  openPrice  closePrice
0   1         10          13
1   2         20          15
>>> df["movement"] = np.sign(df["closePrice"] - df["openPrice"])
>>> df
   id  openPrice  closePrice  movement
0   1         10          13         1
1   2         20          15        -1

这样做的一个好处是,如果openPrice == closePrice,您会自动获得movement == 0,这非常方便。你知道吗

如果你更喜欢手工操作,你可以做向量运算,比如

>>> df["closePrice"] > df["openPrice"]
0     True
1    False
dtype: bool
>>> (df["closePrice"] > df["openPrice"]) * 2 - 1
0    1
1   -1
dtype: int64

因为这里有False == 0True == 1,但是你必须有特殊情况closePrice == openPrice。你知道吗

可以使用where设置设置值的条件,最后一个参数是条件为False时的值:

In [6]:

df['movement'] = np.where(df['openPrice'] < df['closePrice'], 1, -1 )
df
Out[6]:
   id  openPrice  closePrice  movement
0   1         10          13         1
1   2         20          15        -1

[2 rows x 4 columns]

相关问题 更多 >

    热门问题