gota

repository·master·Indexed 25 days ago

https://github.com/go-gota/gota

A 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.

Tokens
1.4K
Snippets
2
Records
8
Agent score
35%

What's inside gota

  1. Chain DataFrame operations

    master
    Gota supports method chaining for wrangling operations. Because chaining can fail, you must check the 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.
  2. Load data into a DataFrame

    master

    You can create DataFrames using several methods depending on your source data:

    1. From Series: Use dataframe.New() by passing multiple series.New() instances.
    2. From Records: Use dataframe.LoadRecords() with a [][]string slice.
    3. From Structs: Use dataframe.LoadStructs() with a slice of arbitrary structs. Note that unexported fields are ignored.
    4. From Maps: Use dataframe.LoadMaps() with a []map[string]interface{}.
    5. From CSV/JSON: Use 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))
  3. Mutate and Update DataFrame values

    master

    Modify 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.
  4. Subset and select data from a DataFrame

    master

    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.
  5. Filter rows in a DataFrame

    master

    Filter rows based on conditions using Filter or FilterAggregation.

    Predefined Operators

    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)

    Logical Combinations

    • AND: Use df.FilterAggregation(dataframe.And, ...).
    • OR: Use df.FilterAggregation(dataframe.Or, ...) or chain multiple .Filter() calls (which act as OR operations by default).

    Custom Comparators

    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")})
  6. Group, Aggregate, and Sort DataFrames

    master

    Perform data wrangling operations:

    • GroupBy: Group data by one or more column names using GroupBy(names...).
    • Aggregation: Apply functions to groups using Aggregation([]AggregationType, []string). Supported types include Aggregation_MAX and Aggregation_MIN.
    • Arrange: Sort the DataFrame using Arrange(...). Use dataframe.Sort(name) for ascending and dataframe.RevSort(name) for descending order.
  7. Series overview

    master

    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.Int
    • series.Float
    • series.String
    • series.Bool