admin管理员组文章数量:1278950
The following renames a column in a Polars-Rust dataframe:
#![allow(unused_variables)]
use polars::prelude::*;
fn main() {
println!("Hello, world!");
let mut df = df! [
"names" => ["a", "b", "c"],
"values" => [1, 2, 3],
].unwrap();
println!("{:?}", df);
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename("names", new_name);
println!("{:?}", df);
}
Suppose now that the name of the first column is unknown (e.g., the dataframe is read from a csv/excel file, where it has no name), and I would like to rename it for the sake of future use:
fn main() {
println!("Hello, world!");
let mut df = df! [
"names" => ["a", "b", "c"],
"values" => [1, 2, 3],
].unwrap();
println!("{:?}", df);
let old_name = df.get_column_names_str()[0];
// let old_name = df.get_column_names_str()[0].clone();
// let old_name = &mut df.get_column_names_str()[0].clone();
// let mut old_name = &mut df.get_column_names_str()[0].clone();
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename(old_name, new_name);
println!("{:?}", df);
}
This results in an error
error[E0502]: cannot borrow `df` as mutable because it is also borrowed as immutable
--> src/main.rs:32:13
|
27 | let old_name = df.get_column_names_str()[0];
| -- immutable borrow occurs here
...
32 | let _ = df.rename(old_name, new_name);
| ^^^------^^^^^^^^^^^^^^^^^^^^
| | |
| | immutable borrow later used by call
| mutable borrow occurs here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `rename_col` (bin "rename_col") due to 1 previous error
It is more or less clear why the error occurs, but not clear how to fix it...
The commented lines show my various unsuccessful attempts at resolving this issue.
Related:
In Rust, how to rename all columns of a Polars Dataframe?
How to rename column names with first row in polars?
The following renames a column in a Polars-Rust dataframe:
#![allow(unused_variables)]
use polars::prelude::*;
fn main() {
println!("Hello, world!");
let mut df = df! [
"names" => ["a", "b", "c"],
"values" => [1, 2, 3],
].unwrap();
println!("{:?}", df);
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename("names", new_name);
println!("{:?}", df);
}
Suppose now that the name of the first column is unknown (e.g., the dataframe is read from a csv/excel file, where it has no name), and I would like to rename it for the sake of future use:
fn main() {
println!("Hello, world!");
let mut df = df! [
"names" => ["a", "b", "c"],
"values" => [1, 2, 3],
].unwrap();
println!("{:?}", df);
let old_name = df.get_column_names_str()[0];
// let old_name = df.get_column_names_str()[0].clone();
// let old_name = &mut df.get_column_names_str()[0].clone();
// let mut old_name = &mut df.get_column_names_str()[0].clone();
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename(old_name, new_name);
println!("{:?}", df);
}
This results in an error
error[E0502]: cannot borrow `df` as mutable because it is also borrowed as immutable
--> src/main.rs:32:13
|
27 | let old_name = df.get_column_names_str()[0];
| -- immutable borrow occurs here
...
32 | let _ = df.rename(old_name, new_name);
| ^^^------^^^^^^^^^^^^^^^^^^^^
| | |
| | immutable borrow later used by call
| mutable borrow occurs here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `rename_col` (bin "rename_col") due to 1 previous error
It is more or less clear why the error occurs, but not clear how to fix it...
The commented lines show my various unsuccessful attempts at resolving this issue.
Related:
In Rust, how to rename all columns of a Polars Dataframe?
How to rename column names with first row in polars?
2 Answers
Reset to default 2Alternatively you can use df.get_column_names_owned()
function which will return Vec<PlSmallStr>
.
let column_names = df.get_column_names_owned();
let old_name = column_names.first().unwrap();
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename(old_name, new_name);
OR
let old_name = &df.get_column_names_owned()[0];
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename(old_name, new_name);
println!("{:?}", df);
The following works (thanks to a hint from @cafce25):
#![allow(unused_variables)]
use polars::prelude::*;
fn main() {
println!("Hello, world!");
let mut df = df! [
"names" => ["a", "b", "c"],
"values" => [1, 2, 3],
].unwrap();
println!("{:?}", df);
let old_name = df.get_column_names_str()[0].to_owned();
let new_name = <PlSmallStr>::from_str("letters");
let _ = df.rename(&old_name, new_name);
println!("{:?}", df);
}
本文标签: dataframeRenaming a single column with unknown name in PolarsRustStack Overflow
版权声明:本文标题:dataframe - Renaming a single column with unknown name in Polars-Rust - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741267216a2368703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
String
. – cafce25 Commented Feb 24 at 13:29