Problem
I want to read in data into polars
dataframe from mysql
database. I am using sqlx
.
sqlx
generates a vector of Structs for example: Vec<Country>
below:
From sqlx
Docs:
// no traits are needed
struct Country { country: String, count: i64 }
let countries = sqlx::query_as!(Country,
"
SELECT country, COUNT(*) as count
FROM users
GROUP BY country
WHERE organization = ?
",
organization
)
.fetch_all(&pool) // -> Vec<Country>
.await?;
// countries[0].country
// countries[0].count
How do i use this Vec<Country>
to generate a polars dataframe
From polars
Docs:
use polars_core::prelude::*;
let s0 = Series::new("a", &[1i64, 2, 3]);
let s1 = Series::new("b", &[1i64, 1, 1]);
let s2 = Series::new("c", &[2i64, 2, 2]);
let list = Series::new("foo", &[s0, s1, s2]);
let s0 = Series::new("B", [1, 2, 3]);
let s1 = Series::new("C", [1, 1, 1]);
let df = DataFrame::new(vec![list, s0, s1]).unwrap();
Probable Solution
The only solution i can think of is, if i can create a series for every column/Data inside the Country
struct and use those individual series to create a dataframe.
I have no idea how to break down a Vec<Country>
into Vec<country>
and Vec<count>