【问题标题】:How to determine trend in series?如何确定系列的趋势?
【发布时间】:2023-01-14 23:30:16
【问题描述】:
1 2 3 4 Combined Series
0.5 5 10 Nan 0.5, 5, 10 Increaseing
1 2 3 4 1, 2, 3, 4 Increasing
8 5 3 -1 8, 5, 3, -1 Decreasing
4 8 5 3 4, 8, 5, 3 neither

我有一个包含以上列 [1,2,3,4,Combined] 的表格

我如何尝试自动化 python 中组合列的系列确定?

def test(combine):
    return "Increasing." if all(combine[i] < combine[i + 1] for i in range(len(combine) - 1)) 
else 
        "Decreasing." if all(combine[i + 1] < combine[i] for i in range(len(combine) - 1)) 
else 
        "neither!"

但这给我带来了结果“0”的错误

【问题讨论】:

  • 错误信息是什么?列表中的Combined 是什么。
  • @WingedSeal,这是一个关键错误“KeyError:0' Combined 只是我创建的结合列 1,2,3,4 的列
  • 我无法重现错误,它工作得很好。除非您将除 int 列表之外的其他内容传递给测试函数。

标签: python jupyter-notebook


【解决方案1】:

你可以尝试这样的事情:

def trend_comment(a):
    s = pd.Series(a)
    return 'increasing' if s.is_monotonic_increasing 
        else 'decreasing' if s.is_monotonic_decreasing 
        else 'neither'

>>> df['Combined'].apply(trend_comment)
0    increasing
1    increasing
2    decreasing
3       neither

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多