capturegraph.data.containers.utilities.columnize
#
columnize - Transform List of Records to Dict of Columns#
Transform a List of dicts/objects into a Dict of Lists for DataFrame creation.
Example
columnize(data)
#
Transform a List of dicts/objects into a Dict of Lists (column-oriented).
Takes a List of records and transposes it into a Dict of columns, where nested dicts/objects are flattened with dot-separated keys. All output lists have the same length (one entry per input item).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A List of dicts or objects with attributes. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, List[Any]]
|
Dict mapping dot-separated keys to Lists of values. |
Example
sessions = List([
Dict({"date": "2024-01-01", "weather": Dict({"temp": 20, "humidity": 0.5})}),
Dict({"date": "2024-01-02", "weather": Dict({"temp": 22, "humidity": 0.6})}),
])
columnize(sessions)
# Dict({
# 'date': ['2024-01-01', '2024-01-02'],
# 'weather.temp': [20, 22],
# 'weather.humidity': [0.5, 0.6]
# })
# Convert to DataFrame
import pandas as pd
df = pd.DataFrame(columnize(sessions))