Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - How to apply If Else statements in Pandas Dataframe?

I have a Pandas Dataframe and I am trying to add a new column where the value in the new column is dependant on some conditions in the existing Dataframe. The Dataframe I have is as follows:

Date / Time Open High Low Close Volume
2020-06-02 16:30:00 1.25566 1.25696 1.25439 1.25634 2720
2020-06-02 17:00:00 1.25638 1.25683 1.25532 1.25614 2800
2020-06-02 17:30:00 1.25615 1.25699 1.25520 1.25565 2827
2020-06-02 18:00:00 1.25565 1.25598 1.25334 1.25341 2993
2020-06-02 18:30:00 1.25341 1.25385 1.25272 1.25287 1899
2020-07-03 07:00:00 1.24651 1.24673 1.24596 1.24603 600
2020-07-03 07:30:00 1.24601 1.24641 1.24568 1.24594 487
2020-07-03 08:00:00 1.24593 1.24618 1.24580 1.24612 455
2020-07-03 08:30:00 1.24612 1.24667 1.24603 1.24666 552
2020-07-03 09:00:00 1.24666 1.24785 1.24623 1.24765 922

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

1 Reply

0 votes
by (71.8m points)

Try:

df['Signal'] = np.where((df['Open'] <= df['Close'].shift(1)) &
                        (df['Close'] > df['Open'].shift(1))  &
                        (df['Close'].shift(1) < df['Open'].shift(1)),
                        'Open', '0')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...