【问题标题】:Julia DataFrame multiple values filteringJulia DataFrame 多值过滤
【发布时间】:2016-01-17 01:37:49
【问题描述】:

有两种过滤DataFrame的方法:

1. df = df[((df[:field].==1) | (df[:field].==2)), :]
2. df = df[[in(v, [1, 2]) for v in df[:field]], :]

第二种方法速度较慢,但​​适用于条件中的可变值集。 有没有我遗漏的语法糖,所以我可以像第一种方式一样快,但有一些 in-like 构造?

【问题讨论】:

    标签: julia syntactic-sugar


    【解决方案1】:
    julia> using DataFrames
    

    findin 函数可能是完成任务的另一种方式:

    julia> function t_findin(df::DataFrames.DataFrame)
            df[findin(df[:A],[1,2]), :]
           end
    t3 (generic function with 1 method)
    

    数组推导:

    julia> function t_compr(df::DataFrames.DataFrame)
            df[[in(v, [1, 2]) for v in df[:A]], :]
           end
    t1 (generic function with 1 method)
    

    多个条件:

    julia> function t_mconds(df::DataFrames.DataFrame)
            df[((df[:A].==1) | (df[:A].==2)), :]
           end
    t2 (generic function with 1 method)
    

    测试数据

    julia> df[:B] = rand(1:30,10_000_000);
    julia> df[:A] = rand(1:30,10_000_000);
    

    测试结果

    julia> @time t_findin(df);
      0.489064 seconds (67 allocations: 19.340 MB, 0.49% gc time)
    
    julia> @time t_mconds(df);
      0.222389 seconds (106 allocations: 78.933 MB, 5.98% gc time)
    
    julia> @time t_compr(df);
     23.634846 seconds (100.00 M allocations: 2.563 GB, 1.47% gc time)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 2018-11-19
    • 2021-02-13
    • 2014-08-12
    • 2016-05-13
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多