Closed phamthanhlam closed 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.")
}
}
Thank you very much
I have a data frame like this
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