MCPcopy Create free account
hub / github.com/apache/datafusion / drop_columns

Method drop_columns

datafusion/core/src/dataframe/mod.rs:468–504  ·  view source on GitHub ↗

Returns a new DataFrame containing all columns except 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?; // +----+----+----+ // | a | b | c |

(self, columns: &[T])

Source from the content-addressed store, hash-verified

466 /// # }
467 /// ```
468 pub fn drop_columns<T>(self, columns: &[T]) -> Result<DataFrame>
469 where
470 T: Into<Column> + Clone,
471 {
472 let fields_to_drop = columns
473 .iter()
474 .flat_map(|col| {
475 let column: Column = col.clone().into();
476 match column.relation.as_ref() {
477 Some(_) => {
478 // qualified_field_from_column returns Result<(Option<&TableReference>, &FieldRef)>
479 vec![self.plan.schema().qualified_field_from_column(&column)]
480 }
481 None => {
482 // qualified_fields_with_unqualified_name returns Vec<(Option<&TableReference>, &FieldRef)>
483 self.plan
484 .schema()
485 .qualified_fields_with_unqualified_name(&column.name)
486 .into_iter()
487 .map(Ok)
488 .collect::<Vec<_>>()
489 }
490 }
491 })
492 .collect::<Result<Vec<_>, _>>()?;
493 let expr: Vec<Expr> = self
494 .plan
495 .schema()
496 .fields()
497 .into_iter()
498 .enumerate()
499 .map(|(idx, _)| self.plan.schema().qualified_field(idx))
500 .filter(|(qualifier, f)| !fields_to_drop.contains(&(*qualifier, f)))
501 .map(|(qualifier, field)| Expr::Column(Column::from((qualifier, field))))
502 .collect();
503 self.select(expr)
504 }
505
506 /// Expand multiple list/struct columns into a set of rows and new columns.
507 ///

Calls 15

collectMethod · 0.80
qualified_fieldMethod · 0.80
ColumnClass · 0.50
iterMethod · 0.45
intoMethod · 0.45
cloneMethod · 0.45
as_refMethod · 0.45
mapMethod · 0.45
into_iterMethod · 0.45
schemaMethod · 0.45
filterMethod · 0.45