【问题标题】:How can I use multiple columns of Pandas expanding() separately when applying functions with multiple args?应用具有多个参数的函数时,如何分别使用多列 Pandas 扩展()?
【发布时间】:2022-10-18 19:21:22
【问题描述】:

我有一个 Pandas DataFrame,包含“时间”和“当前”列。它还有很多其他列,但我不想将它们用于此操作。所有值都是浮点数。

df[['time','current']].head()

     time  current
1     0.0      9.6
2   300.0      9.3
3   600.0      9.6
4   900.0      9.5
5  1200.0      9.5

我想计算电流随时间的滚动积分,这样在每个时间点,我都会得到电流随时间变化的积分。 (我意识到这个特定的操作很简单,但它是一个例子。我不是真的在寻找这个功能,而是整个方法)

理想情况下,我可以做这样的事情:

df[['time','current']].expanding().apply(scipy.integrate.trapezoid)

或者

df[['time','current']].expanding(method = 'table').apply(scipy.integrate.trapezoid)

但这些都不起作用,因为我想将“时间”列作为函数的第一个参数,将“当前”作为第二个参数。该函数确实适用于一列(仅当前),但我不喜欢之后分别除以时间步长。

在expanding().apply() 中似乎无法访问DataFrame 列。 我听说在内部扩展被视为一个数组,所以我也试过这个:

df[['time','current']].expanding(method = 'table').apply(lambda x:scipy.integrate.trapezoid(x[0], x[1]))


df[['time','current']].expanding(method = 'table').apply(lambda x:scipy.integrate.trapezoid(x['time'], x['current']))

和变体,但我永远无法访问扩展()中的列。

事实上,即使在普通 DataFrame 上使用 apply() 也不允许同时使用列,因为每个列都被顺序视为一个系列。

df[['time','current']].apply(lambda x:scipy.integrate.trapezoid(x.time,x.current))

...

AttributeError: 'Series' object has no attribute 'time'

This answer 提到了用于扩展()的方法“表”,但当时还没有出来,我似乎无法弄清楚它在这里需要什么。他们的解决方案只是手动完成。

我也尝试过先定义函数,但这也会返回错误:

def func(x,y):
    return(scipy.integrate.trapezoid(x,y))

df[['time','current']].expanding().apply(func)

...

DataError: No numeric types to aggregate

扩展().apply()甚至可以实现我的要求吗?我应该换一种方式吗?我可以申请扩展吗里面应用()?

谢谢,祝你好运。

【问题讨论】:

  • scipy.integrate.cumtrapz 已经是累积(扩展)计算,所以就使用它?
  • @ALollz 我不知道,我会调查一下。但这并不能真正解决一般问题。不过谢谢。

标签: python pandas dataframe apply


【解决方案1】:

概述

它尚未在 pandas 中完全实现,但您可以采取一些措施来解决问题。 expanding()rolling() 加上 .agg().apply() 将逐列处理,除非您精确到 method='table',(参见方法 2)。

方法一

只要您输出一列,就有一种解决方法可以得到您想要的东西。诀窍是将列移动到索引,然后在函数中重置它:(不要使用scipy.integrate.trapezoid 这样做,因为正如@ALollz 所说scipy.integrate.cumtrapz 已经是累积(扩展)计算)

def custom_func(serie):
   subDf = serie.reset_index()
   # work with the sub dataframe as you would do in a groupby
   # you have access to subDf.x and subDf.y
   return(scipy.integrate.trapezoid(subDf.x,subDf.y))

df.set_index(['y']).expanding().agg(custom_func)

方法二

您可以在expanding() 中使用method='table'(可从pandas==1.3.0 获得) 和rolling() 在这种情况下,您需要使用.apply(custom_func, raw=True,engine='numba') 并在numba python 中编写一个函数custom_func(注意类型),它将采用您的数据帧的numpy 数组表示。如果你这样做,你的custom_func 需要输出一个长度为输入的数组,因此你可能必须在输入中添加虚拟列以绕过它并在之后重命名你的列。

min_periods=100

def custom_func(table):
    rep = np.zeros(len(table))
    # You need something like this if you want to use the min_periods argument
    if len(table) < min_periods :
        return rep
    # Do something with your numpy arrays
    return rep 

df.expanding(min_periods,method='table').apply(custom_func,raw=True,engine='numba')

# Rename
df.columns = ...

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 2018-09-25
    • 2013-11-23
    • 2017-12-16
    • 1970-01-01
    • 2015-04-25
    • 2015-04-28
    • 2011-10-13
    相关资源
    最近更新 更多