【问题标题】:How to calculate the maximum of several columns through Vaex?如何通过Vaex计算几列的最大值?
【发布时间】:2022-02-03 23:21:43
【问题描述】:

我想有效地计算一个非常大的数据集中几列的最大值(axis=1),而我现在使用的代码是:df["ia_timestamp"] = df[labels].values.max(axis=1)。这里 df 是 Vaex 中的 DataFrame。
我认为将“值”转换为 numpy.array 的步骤很耗时,那么有更好的方法吗?

【问题讨论】:

    标签: python vaex


    【解决方案1】:

    vaex 提供的max 方法正在计算列的最大值,在您的情况下,您希望每行都有最大值。

    为了计算这个,您可以使用apply 方法,这是一个使用 vaex 3.0.0 的示例:

    import vaex
    import pandas as pd
    
    df = pd.DataFrame(
        {
            "c1": [1, 2, 3, 4],
            "c2": [2, 3, 4, 1]
        }
    )
    
    df_vaex = vaex.from_pandas(df)
    
    df_vaex.apply(lambda *x: max(x), arguments=["c1", "c2"])
    

    它会为您提供预期的输出:

    Expression = lambda_function_3(c1, c2)
    Length: 4 dtype: int64 (expression)
    -----------------------------------
    0  2
    1  3
    2  4
    3  4
    

    注意:我在x 之前使用了*,以使其可用于任意数量的列。如果您有固定数量的列,则可以使用以下内容:

    df_vaex.apply(lambda c1, c2: max(c1, c2), arguments=["c1", "c2"])
    

    在您的情况下,您将不得不使用:

    df["ia_timestamp"] = df.apply(lambda *x: max(x), arguments=labels)
    

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 2021-09-17
      • 2014-03-17
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      相关资源
      最近更新 更多