Search code examples
f#deedle

Simple key based lookups in Deedle Frame


Suppose we have some data as follows:

name        age   eats
bugs bunny  20    carrots
elmer fudd  50    wabbits

What is the Deedle equivalent of the following Pandas example?

>>> df[df["name"] == "bugs bunny"]["eats"]
0    carrots
Name: eats, dtype: object

or

>>> df[df["eats"] == "carrots"]["name"]
0    bugs bunny
Name: name, dtype: object

If there is another more F-sharpy way to do these kinds of lookups (e.g. using records) that would also be very useful.

Thanks.

[EDIT] I guess using records it would be as follows:

type PersonRec = {name : string; age : int ; eats : string}
let rec1 = { name = "bugs bunny"; age = 20; eats = "carrots" }
let rec2 = { name = "elmer fudd"; age = 50; eats = "wabbits" }

let bugsbunnyeats =
    [rec1; rec2] 
    |> List.filter (function
                        | {name = "bugs bunny"}   -> true
                        | _                       -> false
                    )
bugsbunnyeats.Head.eats

But I would still like to see the same operation using Deedle if possible.


Solution

  • Deedle has concept of row & column keys - this makes it super easy to perform lookup based on the key, but for lookup based on other columns/rows, you'll need to use filtering.

    Given your sample data:

    let df = 
      frame [ 
        "age" =?> series [ "bugs bunny" => 20; "elmer fudd" => 50 ]
        "eats" =?> series [ "bugs bunny" => "carrots"; "elmer fudd" => "wabbits" ] ]
    

    This creates a frame:

                  age eats    
    bugs bunny -> 20  carrots 
    elmer fudd -> 50  wabbits 
    

    Now you can use various lookups:

    // Lookup using column & row keys
    df.["eats", "bugs bunny"]
    
    // Lookup using row key
    df.Rows.["bugs bunny"].GetAs<int>("age")
    df.Rows.["bugs bunny"]?age // for numbers
    
    // Lookup using filtering
    let carrotEaters = 
      df.Rows
      |> Series.filter (fun k row -> row.GetAs("eats") = "carrots")
    
    carrotEaters.FirstKey() // bugs bunny
    carrotEaters.FirstValue().GetAs<int>("age") // 20