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
+4.6k Golang : A program that contain another program and executes it during run-time
+14.2k How to automatically restart your crashed Golang server
+8.8k Golang : Serving HTTP and Websocket from different ports in a program example
+35.8k Golang : How to split or chunking a file to smaller pieces?
+12.3k Golang : HTTP response JSON encoded data
+15k nginx: [emerg] unknown directive "ssl"
+17.3k Golang : [json: cannot unmarshal object into Go value of type]
+5.4k Fix fatal error: evacuation not done in time problem
+11.1k Golang : Post data with url.Values{}
+10k Golang : Embed secret text string into binary(executable) file
+7.2k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+4.8k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version