Filter the DataFrame by column. Returns a new DataFrame only containing the specified columns. ``` # use datafusion::prelude::*; # use datafusion::error::Result; # use datafusion_common::assert_batches_sorted_eq; # #[tokio::main] # async fn main() -> Result<()> { let ctx = SessionContext::new(); let df = ctx .read_csv("tests/data/example.csv", CsvReadOptions::new()) .await?; let df = df.select_co
(self, columns: &[&str])
| 328 | /// # } |
| 329 | /// ``` |
| 330 | pub fn select_columns(self, columns: &[&str]) -> Result<DataFrame> { |
| 331 | let fields = columns |
| 332 | .iter() |
| 333 | .map(|name| { |
| 334 | let fields = self |
| 335 | .plan |
| 336 | .schema() |
| 337 | .qualified_fields_with_unqualified_name(name); |
| 338 | if fields.is_empty() { |
| 339 | Err(unqualified_field_not_found(name, self.plan.schema())) |
| 340 | } else { |
| 341 | Ok(fields) |
| 342 | } |
| 343 | }) |
| 344 | .collect::<Result<Vec<_>, _>>()? |
| 345 | .into_iter() |
| 346 | .flatten() |
| 347 | .collect::<Vec<_>>(); |
| 348 | let expr: Vec<Expr> = fields |
| 349 | .into_iter() |
| 350 | .map(|(qualifier, field)| Expr::Column(Column::from((qualifier, field)))) |
| 351 | .collect(); |
| 352 | self.select(expr) |
| 353 | } |
| 354 | /// Project arbitrary list of expression strings into a new `DataFrame`. |
| 355 | /// Method will parse string expressions into logical plan expressions. |
| 356 | /// |