Golang : Accessing dataframe-go element by row, column and name example
Dataframe-go is similar to Python's pandas
package and it is used for statistics and data manipulation inside a Go program. Use this package together with Gonum
and you can do a lot more fun stuff such as predicting a trend momentum, etc..
The code below is just a simple example from my own future reference. Basically it shows how to get an element data by row and column or get a series by name.
Here you go!
package main
import (
"fmt"
"github.com/rocketlaunchr/dataframe-go"
)
func main() {
s1 := dataframe.NewSeriesInt64("day", nil, 1, 2, 3, 4, 5, 6, 7, 8)
s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, 23.4, 56.2, nil, nil, 84.2, 72, 89)
df := dataframe.NewDataFrame(s1, s2)
fmt.Print(df)
// access element by row and column
// get the first item value from SALES column
fmt.Println(df.Series[1].Value(0))
// get the first item value from DAY column
fmt.Println(df.Series[0].Value(7))
// instead of using Series[1] and Series[0]
// we can extract the int value from NameToColumn and use it to get
// the same result
salesSeries, err := df.NameToColumn("sales") // case sensitive
if err != nil {
fmt.Println(err)
} else {
// do a type assertion of float64 on the interface{} result
diff := df.Series[salesSeries].Value(1).(float64) - df.Series[salesSeries].Value(0).(float64)
fmt.Println("Delta : ", diff)
}
}
Output:
+-----+-------+---------+
| | DAY | SALES |
+-----+-------+---------+
| 0: | 1 | 50.3 |
| 1: | 2 | 23.4 |
| 2: | 3 | 56.2 |
| 3: | 4 | NaN |
| 4: | 5 | NaN |
| 5: | 6 | 84.2 |
| 6: | 7 | 72 |
| 7: | 8 | 89 |
+-----+-------+---------+
| 8X2 | INT64 | FLOAT64 |
+-----+-------+---------+
50.3
8
Delta : -26.9
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.7k Golang : Decode XML data from RSS feed
+11.1k Golang : How to flush a channel before the end of program?
+6.8k Golang : Gargish-English language translator
+37.2k Upload multiple files with Go
+5.1k Golang : Get FX sentiment from website example
+7.5k Golang : Test if an input is an Armstrong number example
+8.6k Golang : Get final balance from bit coin address example
+18.8k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+13.3k Golang : Strings comparison
+12.9k Golang : List objects in AWS S3 bucket
+11.1k Golang : Post data with url.Values{}