|
| | Table ()=default |
| | Construct an empty table.
|
| |
| const std::vector< Row > & | rows () const |
| | Get all rows in the table.
|
| |
| const std::set< std::string > & | columns () const |
| | Get all column names in the table.
|
| |
| void | add_row (const Row &row_data) |
| | Add a row to the table. Adds "id" and "timestamp" columns automatically.
|
| |
| Table | select_columns (const std::vector< std::string > &selected_columns) const |
| | Select a subset of columns from the table.
|
| |
| Table | query_rows_not_none (const std::string &column) const |
| | Query rows where a column is not None.
|
| |
| Table | query_rows_equals (const std::string &column, const Value &value) const |
| | Query rows where a column equals a value.
|
| |
| Table | query_rows_where (const std::string &column, const std::function< bool(const Value &) > &predicate) const |
| | Query rows where a column satisfies a predicate.
|
| |
| template<typename RawType > |
| std::vector< RawType > | column_as_vector (const std::string &column) const |
| | Returns the values of a column in a vector.
|
| |
| const Table & | print_pretty (std::ostream &os=logroot) const |
| | Print the table in a pretty formatted style.
|
| |
| const Table & | print_jsonl (std::ostream &os=logroot) const |
| | Print the table as JSON lines. Each row is a JSON object, one per line.
|
| |
| const Table & | print_csv (std::ostream &os=logroot) const |
| | Print the table as CSV.
|
| |
| void | clear () |
| | Clear all rows and columns from the table.
|
| |
Table class for storing and manipulating tabular data.
Rows are stored as maps from column names to values. Columns are dynamically added as needed. Think: each row is a dict-like type. Columns are essentially keys.
- Note
- Each row automatically gets an "id" and "timestamp" column.
Provides functionality to add rows, select columns, query data, and print in various formats (pretty, JSON lines, CSV).
Not optimized for performance but designed for ease of use and flexibility. If you need high performance, or millions of rows, consider using a database or specialized library. But still useful for small to medium datasets, logging, prototyping, and data analysis tasks.
Supports various value types, including strings, numbers, booleans, and None (null).
- Note
- For logging, the convention is that most functions use the key "tag" to add some keyword to the table. To later sort data, therefore add a "tag" to mark where the data comes from.
Example usage:
table.
add_row({{
"name",
"Charlie"}, {
"age", 28}, {
"active",
true}});
table.
add_row({{
"name",
"Alice"}, {
"age", 30}, {
"active",
false}});
table.
add_row({{
"name",
"Bob"}, {
"age", 25}, {
"active",
true}});
table.
add_row({{
"city",
"Berlin"}, {
"country",
"Germany"}, {
"population", 3769000}});
return std::holds_alternative<int>( value ) && std::get<int>( value ) > 26;
});
std::ofstream file("output.csv");
file.close();
std::ofstream json_file("output.jsonl");
json_file.close();
Table class for storing and manipulating tabular data.
Definition table.hpp:84
const Table & print_jsonl(std::ostream &os=logroot) const
Print the table as JSON lines. Each row is a JSON object, one per line.
Definition table.hpp:360
void clear()
Clear all rows and columns from the table.
Definition table.hpp:429
Table query_rows_where(const std::string &column, const std::function< bool(const Value &) > &predicate) const
Query rows where a column satisfies a predicate.
Definition table.hpp:223
const Table & print_csv(std::ostream &os=logroot) const
Print the table as CSV.
Definition table.hpp:404
Table select_columns(const std::vector< std::string > &selected_columns) const
Select a subset of columns from the table.
Definition table.hpp:160
void add_row(const Row &row_data)
Add a row to the table. Adds "id" and "timestamp" columns automatically.
Definition table.hpp:140
const Table & print_pretty(std::ostream &os=logroot) const
Print the table in a pretty formatted style.
Definition table.hpp:289
| const Table & terra::util::Table::print_jsonl |
( |
std::ostream & |
os = logroot | ) |
const |
|
inline |
Print the table as JSON lines. Each row is a JSON object, one per line.
Example output: {"id":1,"timestamp":"2024-06-10 12:34:56","name":"Alice","age":30} {"id":2,"timestamp":"2024-06-10 12:34:57","name":"Bob","age":25}
To parse with pandas: import pandas as pd df = pd.read_json("yourfile.jsonl", lines=True)
- Parameters
-
- Returns
- Reference to this table.