简体   繁体   English

根据多个条件分配值

[英]Assign values based on multiple conditions

My dataframe looks like this:我的数据框如下所示:

     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

What I would need in fact is to modify the colmun status based on the values of the column amount and amount_f .我实际上需要的是根据列amountamount_f的值修改 colmun 状态。 It would be an if statement as follow:这将是一个 if 语句,如下所示:

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

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

  • else df.status = partially_filled

How could I go about this?我该怎么办?

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:您可以为此使用np.select ,它允许您根据条件列表的结果从值列表 ( choicelist ) 中进行选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM