pandas dataframe 新列的条件填充

发布于 2025-01-12 07:47:26 字数 1558 浏览 1 评论 0原文

我正在研究 pandas DataFrame 中的列(趋势)的操作。下面是我的源数据框。目前我已将其设置为 0。

在此处输入图像描述

我想要用于填充趋势列的逻辑位于

  1. if df['Close'] > 下方df.shift(1)['向下'] 然后 1

  2. if df['Close']

    df.shift(1)['Up'] 然后 -1

  3. 如果上述任何一个条件不满足,则 df.shift(1)['Trend']。如果该值为 NaN,则将其设置为 1。

以上纯文本代码,

  1. 如果当前收盘价大于 Down 列的上一行值,则
  2. 如果当前收盘价小于 列的上一行值, 向上列然后 -1
  3. 则为 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.

enter image description here

The logic I want to use to populate Trend column is below

  1. if df['Close'] > df.shift(1)['Down'] then 1

  2. if df['Close'] < df.shift(1)['Up'] then -1

  3. 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,

  1. if current close is greater then previous row value of Down column then 1
  2. if current close is less than previous row value of Up column then -1
  3. 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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2025-01-19 07:47:26

我们可以使用 numpy.select 根据满足的条件来选择值。然后将 numpy.select 的结果传递给 fillna 以用它填充缺失的“趋势”值(这用于不丢失现有的“趋势”值)。然后,由于 NaN 趋势值必须用之前的“Trend”值填充,因此我们使用 ffill 并用 1 填充剩余的 NaN 值。

import numpy as np
df['Trend'] = (df['Trend'].replace(0, np.nan)
               .fillna(pd.Series(np.select([df['Close'] > df['Down'].shift(), 
                                            df['Close'] < df['Up'].shift()],
                                           [1, -1], np.nan), index=df.index))
               .ffill().fillna(1))

输出:

    Close        Up      Down  Trend
0   3.138       NaN       NaN    1.0
1   3.141       NaN       NaN    1.0
2   3.141       NaN       NaN    1.0
3   3.130       NaN       NaN    1.0
4   3.110       NaN       NaN    1.0
5   3.130  3.026432  3.214568    1.0
6   3.142  3.044721  3.214568    1.0
7   3.140  3.047010  3.214568    1.0
8   3.146  3.059807  3.214568    1.0
9   3.153  3.064479  3.214568    1.0
10  3.173  3.080040  3.214568    1.0
11  3.145  3.080040  3.214568    1.0
12  3.132  3.080040  3.214568    1.0
13  3.131  3.080040  3.209850    1.0
14  3.141  3.080040  3.209850    1.0
15  3.098  3.080040  3.205953    1.0
16  3.070  3.080040  3.195226   -1.0

We could use numpy.select to select values depending on which condition is satisfied. Then pass the outcome of numpy.select to fillna 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 use ffill and fill the remaining NaN values with 1.

import numpy as np
df['Trend'] = (df['Trend'].replace(0, np.nan)
               .fillna(pd.Series(np.select([df['Close'] > df['Down'].shift(), 
                                            df['Close'] < df['Up'].shift()],
                                           [1, -1], np.nan), index=df.index))
               .ffill().fillna(1))

Output:

    Close        Up      Down  Trend
0   3.138       NaN       NaN    1.0
1   3.141       NaN       NaN    1.0
2   3.141       NaN       NaN    1.0
3   3.130       NaN       NaN    1.0
4   3.110       NaN       NaN    1.0
5   3.130  3.026432  3.214568    1.0
6   3.142  3.044721  3.214568    1.0
7   3.140  3.047010  3.214568    1.0
8   3.146  3.059807  3.214568    1.0
9   3.153  3.064479  3.214568    1.0
10  3.173  3.080040  3.214568    1.0
11  3.145  3.080040  3.214568    1.0
12  3.132  3.080040  3.214568    1.0
13  3.131  3.080040  3.209850    1.0
14  3.141  3.080040  3.209850    1.0
15  3.098  3.080040  3.205953    1.0
16  3.070  3.080040  3.195226   -1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文