Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

python - 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. It would be an if statement as follow:

  • 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...