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
+23.5k Find and replace a character in a string in Go
+5.7k Unix/Linux : How to test user agents blocked successfully ?
+9.3k Golang : Get all countries currencies code in JSON format
+5.3k Golang : Intercept, inject and replay HTTP traffics from web server
+7.4k Golang : How to handle file size larger than available memory panic issue
+19.1k Golang : Populate dropdown with html/template example
+36.9k Golang : Converting a negative number to positive number
+13.9k Golang : Google Drive API upload and rename example
+14.6k Golang : Get URI segments by number and assign as variable example
+11.5k Golang : Find age or leap age from date of birth example
+10.9k Golang : Create S3 bucket with official aws-sdk-go package
+20.4k Golang : Pipe output from one os.Exec(shell command) to another command