如何在数据框中的 apply() 中使用 shift() 并仍然访问完整系列? [英] How to use shift() within apply() in dataframe and still access full series?

查看:22
本文介绍了如何在数据框中的 apply() 中使用 shift() 并仍然访问完整系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,我正在尝试基于将 lambda 应用于两列来创建一个新列.

I have a dataframe where I am trying to create a new column based on applying a lambda to two columns.

closeunadj  qtr_timedelta
date        
2021-05-18  128.75  107
2021-05-19  130.21  108
2021-05-20  132.15  109
2021-05-21  132.30  110
2021-05-24  133.34  113

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1021 entries, 2017-05-01 to 2021-05-24
Data columns (total 2 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   closeunadj     1021 non-null   float64
 1   qtr_timedelta  1021 non-null   int64  
dtypes: float64(1), int64(1)
memory usage: 63.9 KB

lambda 应该使用来自第二列的变化的移位计数值来计算第一列的简单移位回报.

The lambda is supposed to be calculating a simple shifted return on the first column using a changing shift count value from the second column.

final_merge['qtr_gwth'] = final_merge[['closeunadj',
                                       'qtr_timedelta']].apply(lambda x : x['closeunadj'] / x['closeunadj'].shift(x['qtr_timedelta']) - 1, axis=1)

但是,因为 apply() 是逐行运行的,所以我无法让 shift() 访问完整的closeunadj"系列来计算实际的 shift().因此我得到一个AttributeError: 'numpy.float64' object has no attribute ‘shift’"

However, because the apply() is running row by row I cannot get shift() to access the the full ‘closeunadj’ series to calculate the actual shift(). Thus I get a "AttributeError: 'numpy.float64' object has no attribute ‘shift’"

如果我把分母改为

x.loc[:,’closeunadj’].shift(x[‘qtr_timedelta’]) 

为了让 shift() 访问整个系列,我得到IndexingError: Too many indexers"

to try to get shift() to access the whole series I get "IndexingError: Too many indexers"

非常感谢任何帮助或建议!

Any help or suggestions greatly appreciated!

推荐答案

一种方法是使用全系列 final_merge['closeunadj']shiftapply,然后使用 locx.name(即当前行的索引)来获得正确的值.不确定它是最有效的,但因为你的数据帧大约有 1K 行,所以应该没问题

One way is to use the full series final_merge['closeunadj'] for the shift in the apply, then use loc with x.name (that is the index of the current row) to get the right value. Not sure it is the most efficient but because your dataframe is about 1K rows, should be fine

final_merge['qtr_gwth'] = (
    final_merge[['closeunadj', 'qtr_timedelta']]
      .apply(lambda x : x['closeunadj'] / final_merge['closeunadj'].shift(x['qtr_timedelta']).loc[x.name] - 1, 
             axis=1)
)

这篇关于如何在数据框中的 apply() 中使用 shift() 并仍然访问完整系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆