Chain DataFrame operations
masterErr field of the DataFrame at the end of the chain. If an error occurs in any step, subsequent operations in the chain become no-ops.repository·master·Indexed 25 days ago
https://github.com/go-gota/gotaA Go implementation of DataFrames, Series, and data wrangling methods for tabular data manipulation. It provides functionality similar to Python's Pandas, including loading data from CSV, JSON, structs, and maps; filtering, subsetting, and selecting data; grouping, aggregating, and sorting; and performing inner, left, right, and cross joins.
Err field of the DataFrame at the end of the chain. If an error occurs in any step, subsequent operations in the chain become no-ops.You can create DataFrames using several methods depending on your source data:
dataframe.New() by passing multiple series.New() instances.dataframe.LoadRecords() with a [][]string slice.dataframe.LoadStructs() with a slice of arbitrary structs. Note that unexported fields are ignored.dataframe.LoadMaps() with a []map[string]interface{}.dataframe.ReadCSV() or dataframe.ReadJSON() by passing an io.Reader (e.g., strings.NewReader).When loading records, you can configure type detection using dataframe.DetectTypes(bool), dataframe.DefaultType(series.Type), and dataframe.WithTypes(map[string]series.Type).
// From Series
df := dataframe.New(
series.New([]string{"b", "a"}, series.String, "COL.1"),
series.New([]int{1, 2}, series.Int, "COL.2"),
series.New([]float64{3.0, 4.0}, series.Float, "COL.3"),
)
// From Records with custom type configuration
df := dataframe.LoadRecords(
[][]string{
[]string{"A", "B", "C", "D"},
[]string{"a", "4", "5.1", "true"},
},
dataframe.DetectTypes(false),
dataframe.DefaultType(series.Float),
dataframe.WithTypes(map[string]series.Type{
"A": series.String,
"D": series.Bool,
}),
)
// From Structs
type User struct {
Name string
Age int
Accuracy float64
ignored bool // ignored since unexported
}
users := []User{{"Aram", 17, 0.2, true}}
df := dataframe.LoadStructs(users)
// From CSV
df := dataframe.ReadCSV(strings.NewReader(csvStr))Combine two DataFrames using one of the following join types, specifying the key column(s):
InnerJoinLeftJoinRightJoinCrossJoinModify existing data or add new columns:
Set([]int, DataFrame): Update specific rows with new data.Mutate(Series): Replace an existing column or add a new one at the end of the DataFrame using a Series.Use the following methods to reduce the size of your DataFrame:
Subset([]int): Select specific rows by their index.Select([]int): Select specific columns by their index.Select([]string): Select specific columns by their name.Filter rows based on conditions using Filter or FilterAggregation.
Use dataframe.F{columnName, operator, value} with these operators:
series.Eq (Equal)series.Neq (Not Equal)series.Greater (>)series.GreaterEq (>=)series.Less (<)series.LessEq (<=)series.In (In set)df.FilterAggregation(dataframe.And, ...).df.FilterAggregation(dataframe.Or, ...) or chain multiple .Filter() calls (which act as OR operations by default).You can use series.CompFunc with a function of type func(series.Element) bool for complex logic.
// Filter with AND
fil := df.FilterAggregation(
dataframe.And,
dataframe.F{"A", series.Eq, "a"},
dataframe.F{"D", series.Eq, true},
)
// Custom filter (e.g., prefix check)
hasPrefix := func(prefix string) func(el series.Element) bool {
return func (el series.Element) bool {
if el.Type() == series.String {
if val, ok := el.Val().(string); ok {
return strings.HasPrefix(val, prefix)
}
}
return false
}
}
fil := df.Filter(dataframe.F{"A", series.CompFunc, hasPrefix("aa")})Perform data wrangling operations:
GroupBy(names...).Aggregation([]AggregationType, []string). Supported types include Aggregation_MAX and Aggregation_MIN.Arrange(...). Use dataframe.Sort(name) for ascending and dataframe.RevSort(name) for descending order.A Series is a vector of elements of the same type that supports missing values. They are the building blocks for DataFrame columns. Supported types are:
series.Intseries.Floatseries.Stringseries.Bool