我有以下代码来查找数据框中年龄的平均值。
\nlet df = df! [\n "name" => ["panda", "polarbear", "seahorse"],\n "age" => [5, 7, 1],\n].unwrap();\n\nlet mean = df\n .lazy()\n .select([col("age").mean()])\n .collect().unwrap();\n\nprintln!("{:?}", mean);\nRun Code Online (Sandbox Code Playgroud)\n找到平均值后,我想将值提取为f64.
\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 age \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x82\n\xe2\x94\x82 f64 \xe2\x94\x82 -----> how to transform into a single f64 of value 4.333333?\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 4.333333 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n通常,我会做类似df[0,0]提取唯一值的事情。然而,由于 Polars 并不是索引的大力支持者,那么如何使用 Rust Polars 来做到这一点呢?
我正在尝试使用ndarray板条箱进行一些生物信息学,但我似乎无法动态创建矩阵。
我有布尔向量,我想将它们组合成一个二维数组。然而,尝试展平向量并使用并into_shape不能保留元素的正确顺序。
因此,我尝试创建一个空数组并将行连接到其中,但这给了我一个我无法理解的错误。我知道空数组没有相同的维度,但我找不到将空数组转换为正确的类型和维度的方法。
use ndarray::{concatenate, Array, Axis, Ix2};
fn main() {
#[rustfmt::skip]
let vector_of_vectors = vec![
vec![true, false, true],
vec![false, true, false],
];
let mut matrix: Array<bool, Ix2> = ndarray::array![];
for array in vector_of_vectors.iter() {
matrix = concatenate![Axis(0), matrix, Array::from(array.clone())];
}
}
Run Code Online (Sandbox Code Playgroud)
error[E0308]: mismatched types
--> src/main.rs:12:18
|
12 | matrix = concatenate![Axis(0), matrix, Array::from(array.clone())];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 1 element
|
= note: expected …Run Code Online (Sandbox Code Playgroud)