how to filter dataframe base on some columns value in golang?

Viewed 158

input sample:

name,price,pay_time,refund_time
job,19.0,20220622 12:23:23,20220622 13:23:23
kim,0,20220623 12:23:23,20220623 13:23:23

expect sample:

name,price,finnal_time
job,19.0,20220622 12:23:23
kim,0,20220623 13:23:23

The rule is once the price is equal to 0 and we will use the refund_time as final_time, otherwise, pay_time will be use.

Currently I do it by using https://github.com/go-gota/gota my code is show as below:

package frame

import (
    "fmt"
    "github.com/go-gota/gota/dataframe"
    "github.com/go-gota/gota/series"
    "strings"
)

func LoadCSV() {
    csvStr := `
name,price,pay_time,refund_time
job,19.0,20220622 12:23:23,20220622 13:23:23
kim,0,20220623 12:23:23,20220623 13:23:23
`
    df := dataframe.ReadCSV(strings.NewReader(csvStr))
    df = df.Filter(dataframe.F{Colname: "price", Comparator: series.Eq, Comparando: 0})
    fmt.Println("df -->", df)
}

but the output is:


    name     price    pay_time          refund_time
 0: kim      0.000000 20220623 12:23:23 20220623 13:23:23
    <string> <float>  <string>          <string>

and the other line is deleted, is not what I want.

1 Answers

Does it meet your requirements?

package main

import (
    "fmt"
    "strings"

    "github.com/go-gota/gota/series"

    "github.com/go-gota/gota/dataframe"
)

func main() {
    csvStr := `
name,price,pay_time,refund_time
job,19.0,20220622 12:23:23,20220622 13:23:23
kim,0,20220623 12:23:23,20220623 13:23:23
`
    df := dataframe.ReadCSV(strings.NewReader(csvStr))
    df = df.Filter(dataframe.F{Colname: "price", Comparator: series.GreaterEq, Comparando: 0})

    var (
        finalTimeList []string
    )

    for i := 0; i < df.Nrow(); i++ {
        payTime := df.Elem(i, 2).Val().(string)
        price := df.Elem(i, 1).Val().(float64)
        if price == 0 {
            payTime = df.Elem(i, 3).Val().(string)
        }
        finalTimeList = append(finalTimeList, payTime)
    }
    df = df.Mutate(series.New(finalTimeList, series.String, "final_time"))
    df = df.Drop([]string{"pay_time", "refund_time"})
    fmt.Println("df -->", df)
}

Related