Search code examples
f#deedle

f# deedle filter data frame based on a list


I wanted to filter a Deedle dataframe based on a list of values how would I go about doing this?

I had an idea to use the following code below:

  let d= df1|>filterRowValues(fun row -> row.GetAs<float>("ts") = timex)

However the issue with this is that it is only based on one variable, I then thought of combining this with a for loop and an append function:

for i in 0.. recd.length -1 do 
  df2.Append(df1|>filterRowValues(fun row -> row.GetAs<float>("ts") = recd.[i]))

This does not work either however and there must be a better way of doing this without using a for loop. In R I could for instance using an %in%.


Solution

  • You can use the F# set type to create a set of the values that you are interested. In the filtering, you can then check whether the set contains the actual value for the row.

    For example, say that you have recd of type seq<float>. Then you should be able to write:

    let recdSet = set recd
    let d = df1 |> Frame.filterRowValues (fun row -> 
      recdSet.Contains(row.GetAs<float>("ts"))
    

    Some other things that might be useful:

    • You can replace row.GetAs<float>("ts") with just row?ts (which always returns float and works only when you have a fixed name, like "ts", but it makes the code nicer)

    • Comparing float values might not be the best thing to do (because of floating point imprecisions, this might not always work as expected).