相关疑难解决方法(0)

在pandas数据帧中向量化条件赋值

如果我有一个带有列x的数据帧df,并希望在伪代码中使用此值基于x的值创建列y

 if df['x'] <-2 then df['y'] = 1 
 else if df['x'] > 2 then df['y']= -1 
 else df['y'] = 0
Run Code Online (Sandbox Code Playgroud)

我将如何实现这一目标.我认为np.where是最好的方法,但不知道如何正确编码.

python numpy vectorization pandas

22
推荐指数
3
解决办法
2万
查看次数

绘图散点图标记的条件格式

问题:
我有一个包含xy值对、加号lower_limitupper_limit值的数据集y

我想在 plot.ly 散点图中绘制xvs.并将标记着色为绿色,如果? ,否则为红色ylower_limityupper_limit

我知道我可以使用 2 个跟踪,或者color在 DataFrame 中添加一列。但是,我想即时生成这些颜色并仅使用一条痕迹。

示例:
考虑这个数据集:

   x   y  lower_limit  upper_limit
0  1  13           10           15
1  2  13           15           20
2  3  17           15           20
Run Code Online (Sandbox Code Playgroud)

第一个标记 ( x=1, y=13) 应该是绿色的,因为lower_limit? y? upper_limit(10 ? 13 ? 15),就像第三个一样。
但是第二个应该是红色的,因为y< lower_limit

然后我想生成这个图: 在此处输入图片说明


MWE:

import pandas …
Run Code Online (Sandbox Code Playgroud)

python colors plotly plotly-python

2
推荐指数
1
解决办法
3298
查看次数

标签 统计

python ×2

colors ×1

numpy ×1

pandas ×1

plotly ×1

plotly-python ×1

vectorization ×1