如何根据同一数据框中的另一列计算 Pandas 数据框中的值

How can I calculate values in a Pandas dataframe based on another column in the same dataframe

我正在尝试在 Pandas 数据框中创建一个新的值列,这些值是根据同一数据框中的另一列计算得出的:

df['ema_ideal'] = df['Adj Close'].ewm(span=df['ideal_moving_average'], min_periods=0, ignore_na=True).mean

但是,我收到错误消息:

ValueError: The truth of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any(), or a.all().

如果我将 span 设置为 30 或某个整数,则不会收到此错误。另外,ideal_moving_average 是一列浮点数。

我的两个问题是:

为什么我会收到错误消息?

如何将 ideal_moving_average 中的列值合并到 df['ema_ideal'] 列中(子问题,因为我是 Pandas 的新手 - 此列是数据框中的一个系列吗?)

感谢您的帮助!

编辑:显示 Adj Close 数据的示例,格式错误

Date              Open        High         Low       Close   Adj Close                                                                           
2017-01-03  225.039993  225.830002  223.880005  225.240005  222.073914   
2017-01-04  225.619995  226.750000  225.610001  226.580002  223.395081   
2017-01-05  226.270004  226.580002  225.479996  226.399994  223.217606   
2017-01-06  226.529999  227.750000  225.899994  227.210007  224.016220   
2017-01-09  226.910004  227.070007  226.419998  226.460007  223.276779   
2017-01-10  226.479996  227.449997  226.009995  226.460007  223.276779

从另一个数据框创建数据框列绝对没有问题。

您收到的错误完全不同,当您尝试将 Series 与 andornot 等逻辑函数进行比较时,会返回此错误。 .

通常,要避免此错误,您必须明智地比较 Series 元素,例如使用 & 而不是 and,或 ~ 而不是 not,或使用 numpy 进行元素明智的比较。

这里的问题是您尝试使用 Serie 作为 ema 的跨度,而 pandas ewma 函数只接受整数作为跨度。

例如,您可以计算每个可能周期的 ema,然后将它们重新组合到您设置为数据框 ema ideal 列的 Serie 中。

我认为这样的东西对你有用:

df['ema_ideal'] = df.apply(lambda x: df['Adj Close'].ewm(span=x['ideal_moving_average'], min_periods=0, ignore_na=True).mean(), axis=1)

提供 axis=1DataFrame.apply 允许您按需要访问数据行。

对于任何想知道的人来说,问题是 span 不能取多个值,当我试图将 df['ideal_moving_average'] 传递给它时发生了这种情况。相反,我使用了下面的代码,它似乎是逐行将该行的值传递给 span.

df['30ema'] = df['Adj Close'].ewm(span=df.iloc[-1]['ideal_ma'], min_periods=0, ignore_na=True).mean()

编辑:我暂时接受这是正确的,直到有人表明它不起作用或可以创造更好的东西,感谢您的帮助。