根据多个条件分配值 [英] Assign values based on multiple conditions

查看:116
本文介绍了根据多个条件分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框如下:

     timestamp        price       amount amount_f status    eth_amount
0   2018-11-30 13:48:00 0.00348016  10  0   cancelled   0.000000
1   2018-11-30 13:48:00 0.00350065  10  0   cancelled   0.000000
2   2018-11-30 13:50:00 0.00348021  10  0   cancelled   0.000000
3   2018-11-30 13:50:00 0.00350064  10  0   cancelled   0.000000
4   2018-11-30 13:51:00 0.00348054  10  0   cancelled   0.000000
5   2018-11-30 13:51:00 0.00349873  10  0   cancelled   0.000000
6   2018-11-30 13:52:00 0.00348094  10  10  filled  0.034809
7   2018-11-30 13:52:00 0.00349692  10  0   cancelled   0.000000

实际上,我需要根据列amountamount_f的值修改大学状态.它将是一个if语句,如下所示:

What I would need in fact is to modify the colmun status based on the values of the column amount and amount_f. It would be an if statement as follow:

  • df.amount == df.amount_f then df.status = filled

  • df.amount == df.amount_f then df.status = filled

elif df.amount_f == 0 then df.status = cancelled

else df.status = partially_filled

我该怎么办?

推荐答案

您可以使用

You can use np.select for this, which allows you to select from a list of values (choicelist) depending on the results of a list of conditions:

c1 = df.amount == df.amount_f
c2 = df.amount_f == 0
df.loc[:, 'status'] = np.select(condlist=[c1, c2], 
                                choicelist=['filled', 'cancelled'], 
                                default='partially_filled')

        timestamp        price     amount  amount_f  status    eth_amount
0 2018-11-30  13:48:00  0.003480      10         0  cancelled    0.000000
1 2018-11-30  13:48:00  0.003501      10         0  cancelled    0.000000
2 2018-11-30  13:50:00  0.003480      10         0  cancelled    0.000000
3 2018-11-30  13:50:00  0.003501      10         0  cancelled    0.000000
4 2018-11-30  13:51:00  0.003481      10         0  cancelled    0.000000
5 2018-11-30  13:51:00  0.003499      10         0  cancelled    0.000000
6 2018-11-30  13:52:00  0.003481      10        10     filled    0.034809
7 2018-11-30  13:52:00  0.003497      10         0  cancelled    0.000000

这篇关于根据多个条件分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆