Julia DataFrame多值过滤

时间:2016-01-17 01:37:50

标签: julia syntactic-sugar

以下情况有两种方法可以过滤DataFrame:

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

第二种方法较慢,但它适用于条件中可变的值集。 有没有我错过的语法糖,所以我可以像第一种方式一样快速地获得它但是有一些in - 就像构造一样?

1 个答案:

答案 0 :(得分:3)

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)