go-gota / gota

Gota: DataFrames and data wrangling in Go (Golang)
Other
2.98k stars 277 forks source link

Filter dataframe return indexer #63

Closed phamthanhlam closed 6 years ago

phamthanhlam commented 6 years ago

I have a data frame like this

df := dataframe.LoadRecords( [][]string{ []string{"A", "B", "C", "D"}, []string{"a", "4", "5.1", "true"}, []string{"k", "5", "7.0", "true"}, []string{"k", "4", "6.0", "true"}, []string{"a", "2", "7.1", "false"}, }, )

I want to filter column B to a value of 5, I want the return value to be int 2 .

2 is indexer of df

kmrx commented 6 years ago

Hi @phamthanhlam ,

I will reiterate to make sure I understand correctly - you want to filter df by column. The column you want to filter on is column B. You want to get the index of row in dataframe where B has the value you want to filter on.

Assuming you want to start indexing from the column label as 0. You can do something similar to below


import (
    "fmt"
    "strconv"

    "github.com/kniren/gota/dataframe"
)

func main() {
    df := dataframe.LoadRecords(
        [][]string{
            []string{"A", "B", "C", "D"},
            []string{"a", "4", "5.1", "true"},
            []string{"k", "5", "7.0", "true"},
            []string{"k", "4", "6.0", "true"},
            []string{"a", "2", "7.1", "false"},
        },
    )
    colSeries := df.Col("B").Records() // get records from column B into a series type and get array of strings of values from the column series
    findIndexFor := strconv.Itoa(5)    // type conversion
    var indexedAt int
    // default value of indexedAt is 0
    for index, value := range colSeries { // range over array to do a simple boolean check if the value exists
        if value == findIndexFor {
            indexedAt = index + 1
            fmt.Println("Found value at index ", indexedAt)
        }
    }
    if indexedAt == 0 {
        fmt.Println("No value found hence no index available.")
    }
}
phamthanhlam commented 6 years ago

Thank you very much