pandas dataframe 新列的条件填充
我正在研究 pandas DataFrame 中的列(趋势)的操作。下面是我的源数据框。目前我已将其设置为 0。
我想要用于填充趋势列的逻辑位于
if df['Close'] > 下方df.shift(1)['向下'] 然后 1
if df['Close']
df.shift(1)['Up'] 然后 -1
如果上述任何一个条件不满足,则 df.shift(1)['Trend']。如果该值为 NaN,则将其设置为 1。
以上纯文本代码,
- 如果当前收盘价大于 Down 列的上一行值,则
- 如果当前收盘价小于 列的上一行值, 向上列然后 -1
- 则为 1如果其中任何一个条件不满足,则 ,然后设置趋势列的上一行值,只要其不是 NaN。如果其 NaN 则设置为 1
更新
数据作为文本
Close Up Down Trend
3.138 NaN NaN 0
3.141 NaN NaN 0
3.141 NaN NaN 0
3.130 NaN NaN 0
3.110 NaN NaN 0
3.130 3.026432 3.214568 0
3.142 3.044721 3.214568 0
3.140 3.047010 3.214568 0
3.146 3.059807 3.214568 0
3.153 3.064479 3.214568 0
3.173 3.080040 3.214568 0
3.145 3.080040 3.214568 0
3.132 3.080040 3.214568 0
3.131 3.080040 3.209850 0
3.141 3.080040 3.209850 0
3.098 3.080040 3.205953 0
3.070 3.080040 3.195226 0
预期输出
I am working on manipulation of a column(Trend) in pandas DataFrame. Below is my source DataFrame. Currently I have set it to 0.
The logic I want to use to populate Trend column is below
if df['Close'] > df.shift(1)['Down'] then 1
if df['Close'] < df.shift(1)['Up'] then -1
if any one of the above condition does not meet then, df.shift(1)['Trend']. if this value is NaN then set it to 1.
Above code in plainText,
- if current close is greater then previous row value of Down column then 1
- if current close is less than previous row value of Up column then -1
- if any one of those conditions does not meet, then set previous row value of Trend column as long as its not NaN. if its NaN then set to 1
UPDATE
Data as text
Close Up Down Trend
3.138 NaN NaN 0
3.141 NaN NaN 0
3.141 NaN NaN 0
3.130 NaN NaN 0
3.110 NaN NaN 0
3.130 3.026432 3.214568 0
3.142 3.044721 3.214568 0
3.140 3.047010 3.214568 0
3.146 3.059807 3.214568 0
3.153 3.064479 3.214568 0
3.173 3.080040 3.214568 0
3.145 3.080040 3.214568 0
3.132 3.080040 3.214568 0
3.131 3.080040 3.209850 0
3.141 3.080040 3.209850 0
3.098 3.080040 3.205953 0
3.070 3.080040 3.195226 0
Expected output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用 numpy.select 根据满足的条件来选择值。然后将
numpy.select
的结果传递给fillna
以用它填充缺失的“趋势”值(这用于不丢失现有的“趋势”值)。然后,由于 NaN 趋势值必须用之前的“Trend”值填充,因此我们使用ffill
并用 1 填充剩余的 NaN 值。输出:
We could use
numpy.select
to select values depending on which condition is satisfied. Then pass the outcome ofnumpy.select
tofillna
to fill in missing "Trend" values with it (this is used to not lose existing "Trend" values). Then since NaN trend values must be filled with previous "Trend" value, we useffill
and fill the remaining NaN values with 1.Output: