Question

I want to select some rows based on multiple rows' value comparisons.

say the data frame (df) is like:

    one two three   four
a   0   1   2   3
b   4   5   6   7
c   8   9   10  11
d   12  13  14  15

I want to get the rows where col["two"] >= 5 and col["four"] <=11. This can be simply done in python with pandas like:

df[(df["two"]>=5 & df["four"]<=11]

How can I do this in F# with Deedle?

Thanks!

Was it helpful?

Solution

You can use the Frame.filterRows function:

df |> Frame.filterRows (fun k row ->
    row?two >= 5.0 && row?four <= 11.0)

Having something like the pandas slicing syntax would be nice, but we have not found entirely smooth way of doing that yet, so using filter function is the current way of doing that (but the good thing is that this is consistent with other filter operations - for lists and sequences).

Here, I'm using row?two which is a simplified notation for getting numeric (floating point) values from a data frame, but you could use GetAs<T>("two") for values of non-numeric types.

Just for a reference, here is my sample data set:

let df = 
  Frame.ofRows
    [ 'a' => Series.ofValues [ 0;1;2;3]
      'b' => Series.ofValues [ 4;5;6;7]
      'c' => Series.ofValues [ 8;9;10;11]
      'd' => Series.ofValues [ 12;13;14;15] ]
  |> Frame.indexColsWith ["one"; "two"; "three"; "four"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top