I’ve parsed multiple Rust structs from a set of JSON data. What is the recommended way of creating a Polars DataFrame
from a subset of the fields?
For example, if I parsed the following structs, what’s the best way to construct aDataFrame
from them both? Should I create multiple series and append each column to its series and then construct using df!
macro?
struct Parent {
name: String
}
struct ChildA {
parent_name: String,
my_name: String,
// some more fields at this level
}
pseudocode:
series_1
series_2
series_3
for data in db {
append to series_1 for Parent name
append to series_2 for child name
append to series_3 for another field
}
df = df! {
"parent_name" => series_1,
"sub_name" => series_2,
"another_field" => series_3,
};
Is there an easier way? Is this efficient?
I ended up appending values to different Vec
and then created a series for each Vec
in the DataFrame
constructor:
let col1: Vec<String> = vec![];
let col2: Vec<String> = vec![];
let col3: Vec<u8> = vec![];
// later...
let df = DataFrame::new(vec![
Series::new("col1".into(), col1),
Series::new("col2".into(), col2),
Series::new("col3".into(), col3),
])?;