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
+7.7k Golang : Grayscale Image
+7.3k Golang : How to handle file size larger than available memory panic issue
+29.5k Golang : Get and Set User-Agent examples
+9.5k Golang : Load ASN1 encoded DSA public key PEM file example
+4.7k Golang : Calculate a pip value and distance to target profit example
+6.7k Golang : Fibonacci number generator examples
+35.8k Golang : How to split or chunking a file to smaller pieces?
+6k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+16.2k CodeIgniter/PHP : Create directory if does not exist example
+17.1k Golang : Multi threading or run two processes or more example
+21.2k Golang : Encrypt and decrypt data with TripleDES
+10.2k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine