如何在熊猫,python3中将条件与其他行(时间序列数据中的先前时刻)一起使用

光环

我有一个pandas.dataframe df它是一个时间序列数据,具有1000行3列。我想要的在下面的伪代码中给出。

for each row
    if the value in column 'colA' at [this_row-1] is higher than 
    the value in column 'B' at [this_row-2] for more than 3%
        then set the value in 'colCheck' at [this_row] as True.

Finally, pickout all the rows in the df where 'colCheck' are True.

我将使用以下示例进一步说明我的目的。

 df =     
             'colA', 'colB', 'colCheck'
 Dates
 2017-01-01,     20,     30,      NAN
 2017-01-02,     10,     40,      NAN
 2017-01-03,     50,     20,     False
 2017-01-04,     40,     10,      True

首先,当this_row = 2(第三行,日期为colA2017年1月3日)时,[this_row-1]中10的值为colB,[this_row-2]中的值为30因此(10-30)/30 = -67% < 3%,因此colCheck[this_row]中的值为False。

同样地,当this_row = 3(50-40)/40 = 25% > 3%,所以在值colCheck在[this_row]为True。

最后但并非最不重要的一点colCheck是,由于计算需要访问中的[this_row-2] ,因此其中的前两行应为NAN colB但是前两行没有[this_row-2]。

此外,的标准3%和[行-1]中colA,[行-2]在colB仅仅是示例。在我的实际项目中,它们是情境的,例如4%[row-3]。

我正在寻找简洁优雅的方法。我正在使用Python3。

谢谢。

海盗

您可以重新排列数学并使用 pd.Series.shift

df.colA.shift(1).div(df.colB.shift(2)).gt(1.03)

Dates
2017-01-01    False
2017-01-02    False
2017-01-03    False
2017-01-04     True
dtype: bool

使用pd.DataFrame.assign我们可以用新列创建一个副本

df.assign(colCheck=df.colA.shift(1).div(df.colB.shift(2)).gt(1.03))

            colA  colB  colCheck
Dates                           
2017-01-01    20    30     False
2017-01-02    10    40     False
2017-01-03    50    20     False
2017-01-04    40    10      True

如果您坚持将前两个保留为NaN,则可以使用iloc

df.assign(colCheck=df.colA.shift(1).div(df.colB.shift(2)).gt(1.03).iloc[2:])

            colA  colB colCheck
Dates                          
2017-01-01    20    30      NaN
2017-01-02    10    40      NaN
2017-01-03    50    20    False
2017-01-04    40    10     True

为了最大的清晰度:

# This creates a boolean array of when your conditions are met
colCheck = (df.colA.shift(1) / df.colB.shift(2)) > 1.03
# This chops off the first two `False` values and creates a new
# column named `colCheck` and assigns to it the boolean values
# calculate just above.
df.assign(colCheck=colCheck.iloc[2:])

            colA  colB colCheck
Dates                          
2017-01-01    20    30      NaN
2017-01-02    10    40      NaN
2017-01-03    50    20    False
2017-01-04    40    10     True

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Haskell中将fold函数与其他数据类型一起使用

如何在列表理解中与其他操作一起使用print()?

熊猫时间序列:如何仅将一天中的时间(无日期)与其他值作图?

如何在SQL Server中将输出参数与其他列名称一起使用

在笔记本目录中将函数与其他python文件中的变量一起使用

如何在Python 3中将CSV编写器与GZIP文件一起使用?

如何将多个R对象与其他数据一起保存到文件中?

如何将单选/复选框数据与其他字段一起添加到表单中?

如何在TSV中与其他内容一起打印JSON文本?

如何在OpenGL中与其他现有纹理一起创建纹理图集?

如何在Scala中与其他对象一起扩展对象属性?

如何在Xamarin表单中与其他UI元素一起使用ZXing.Net?

如何在Android Studio中复制应用程序以与其他资源一起使用?

将VBA范围命令与其他工作表中的ActiveX按钮一起使用

如何在派生类中将策略模式与其他方法一起使用

如何在布局中将 QCamera 与其他小部件一起排列?

在Jhipster微服务中将用户实体与其他数据一起使用

将已删除的数据与其他数据一起包含在审计查询中

如何在Swift 3中将URLSession与代理一起使用

如何在Swift 3中将getopt与命令行参数一起使用?

如何在Swift 3中将sin(_:)与FloatingPoint值一起使用

如何在OSX 10.6中将MySQLdb与Python和Django一起使用?

当嵌套表与其他数据类型混合在一起时,如何在Lua中循环浏览嵌套表?

在python3中将整数0与其他整数连接起来

如何在DB2中与其他SQL一起运行脚本

如何在AngularJS中与其他参数一起在http.post中上传图像文件

如何将Fn :: FindInMap中的列表与其他项目组合在一起?

如何将按钮的内容与其他XAML页面中的值列表绑定在一起?

将委托的构造函数与其他构造函数中的try-with-resources惯用语一起使用