DataTable is a flexible and efficient data structure for managing two-dimensional data in Go. It provides a variety of methods for data manipulation, querying, and display. The DataTable structure uses a column-based storage model, which allows for efficient column operations and flexible row management.
type DataTable struct {
mu sync.Mutex
columns []*DataList
columnIndex map[string]int
rowNames map[string]int
creationTimestamp int64
lastModifiedTimestamp atomic.Int64
}
- Column-based storage for efficient column operations
- Support for named rows and columns
- Thread-safe operations
- Flexible data manipulation (append, update, delete)
- Advanced querying capabilities
- Timestamp tracking for creation and modification
func NewDataTable(columns ...*DataList) *DataTable
Creates a new DataTable with optional initial columns.
Appends columns to the DataTable
. If the new columns are shorter than existing ones, nil
values will be appended to match the length. If they are longer, existing columns will be extended with nil
values.
Appends rows to the DataTable
using DataList
objects. If the new rows are shorter than existing columns, nil
values will be appended to match the length.
Appends rows to the DataTable
by mapping column indices to values. If necessary, new columns are created, and existing columns are extended with nil
values.
Appends rows to the DataTable
by mapping column names to values. New columns are created if necessary, and existing columns are extended with nil
values.
Returns the element at the specified row and column index. If the indices are out of bounds, it returns nil
.
Returns the element at the specified row and column number index. If the indices are out of bounds, it returns nil
.
Returns a DataList
containing the data of the specified column index.
Returns a DataList
containing the data of the specified column number.
Returns a DataList
containing the data of the specified row index.
Updates the element at the specified row and column index.
Updates the column with the specified index with a new DataList
.
Updates the column at the specified number index with a new DataList
.
Updates the row at the specified index with a new DataList
.
Sets the row names to the values of the specified column and drops the column.
Sets the column names to the values of the specified row and drops the row.
Returns the indices of rows that contain the specified value.
Returns the indices of rows that contain all specified values.
Returns the indices of rows that contain at least one element with the specified substring.
Returns the indices of rows where all elements contain the specified substring.
Returns the names of columns that contain the specified value.
Returns the names of columns that contain all specified values.
Returns the names of columns that contain at least one element with the specified substring.
Returns the names of columns where all elements contain the specified substring.
Drops columns from the DataTable
by their names.
Drops columns from the DataTable
by their indices.
Drops columns from the DataTable
by their number indices.
Drops columns that contain any string elements.
Drops columns that contain any numeric elements.
Drops columns that contain any nil
elements.
Drops rows from the DataTable
by their indices.
Drops rows from the DataTable
by their names.
Drops rows that contain any string elements.
Drops rows that contain any numeric elements.
Drops rows that contain any nil
elements.
Returns a map representing the data in the DataTable
. The keys can be column names if specified.
Prints the content of the DataTable
in a tabular format.
Prints the types of elements in the DataTable
.
Returns the number of rows and columns in the DataTable
.
The first return value is the number of rows, and the second return value is the number of columns.
Returns the number of occurrences of the specified value in the DataTable
.
Returns a map of the number of occurrences of each value in the DataTable
.
Transposes the DataTable
, converting rows into columns and vice versa.
Returns the mean of the DataTable
.
Returns the name of the row at the specified index.
Sets the name of the row at the specified index.
Returns the timestamp when the DataTable
was created.
Returns the timestamp when the DataTable
was last modified.
Returns a new DataTable
with rows that satisfy the given filter function.
Returns a new DataTable
with rows where any element satisfies the custom filter function.
Returns a new DataTable
with rows where the column index is greater than the specified threshold.
Returns a new DataTable
with rows where the column index is greater than or equal to the specified threshold.
Returns a new DataTable
with rows where the column index is less than the specified threshold.
Returns a new DataTable
with rows where the column index is less than or equal to the specified threshold.
Returns a new DataTable
with rows where the column index equals the specified index.
Returns a new DataTable
with rows where the column name equals the specified name.
Returns a new DataTable
with rows where the column name contains the specified substring.
Returns a new DataTable
with rows where the row name equals the specified name.
Returns a new DataTable
with rows where the row name contains the specified substring.
Returns a new DataTable
with rows where the row index is greater than the specified threshold.
Returns a new DataTable
with rows where the row index is greater than or equal to the specified threshold.
Returns a new DataTable
with rows where the row index is less than the specified threshold.
Returns a new DataTable
with rows where the row index is less than or equal to the specified threshold.
Returns a new DataTable
with rows where the row index equals the specified index.
Writes the DataTable
to a CSV file. If setRowNamesToFirstCol
is true
, the first column will be used as row names. If setColNamesToFirstRow
is true
, the first row will be used as column names.
Loads a DataTable
from a CSV file. If setFirstColToRowNames
is true
, the first column will be used as row names. If setFirstRowToColNames
is true
, the first row will be used as column names.
- Use appropriate method calls to manipulate data (e.g., use AppendRowsByName when you have named columns).
- Regularly check for and handle nil values in your data.
- Use the Show() method for debugging and data inspection.
- Monitor logs for warnings and errors during operations.
- Consider the trade-offs between using named rows/columns and index-based access based on your use case.
- When performing multiple operations, consider grouping them to minimize the number of times the lastModifiedTimestamp is updated.