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
+15.2k Golang : How to check if IP address is in range
+17.4k Golang : Get future or past hours, minutes or seconds
+9.7k Golang : Find correlation coefficient example
+12.2k Golang : Simple client-server HMAC authentication without SSL example
+6.4k CodeIgniter : form input set_value cause " to become & quot
+6k PHP : How to check if an array is empty ?
+11.7k Golang : convert(cast) float to string
+6.8k Golang : Calculate pivot points for a cross
+5k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+6.6k Golang : Warp text string by number of characters or runes example
+9.2k Golang : Write multiple lines or divide string into multiple lines
+7.8k Golang : Reverse a string with unicode