filter
Keeps only the rows of a DataFrame that satisfy a row condition. The order of the kept rows doesn't change.
Returns a new DataFrame with the rows that satisfy the condition. If no row satisfies it, the result is an empty DataFrame with the same columns.
The row condition receives each row both as this and as it, so the values of the row can be accessed directly using Extension Properties or String column accessors, including the values of nested columns of a ColumnGroup.
See also:
drop, which removes the rows that satisfy a row condition.distinct, which removes duplicate rows.takeWhile/dropWhile, which keep or remove only the first rows that satisfy a condition.Filter rows, an overview of the operations that filter rows.
API reference: filter
Parameters
predicate: DataRow.(DataRow) -> Boolean— a row condition that returnstrueif the row should be kept in the result.
Examples
The following dataframe will be used in the examples below. The rows that satisfy the condition from the next example are highlighted:
Keep only the people older than 18 whose first name starts with "A":
The row can also be taken as an explicit lambda parameter instead of it.
Besides the row values, the row condition can use the DataRow API — for example, the index of the row and the previous row. Keep the people starting from the third row whose previous person is happy:
filter on DataColumn
Keeps only the values of a DataColumn that satisfy the given predicate.
Returns a new DataColumn with the same name, containing only the matching values in their original order.
Parameters
predicate: (value) -> Boolean— a lambda that receives a value of the column and returnstrueif the value should be kept in the result.
Examples
Keep only the ages greater than 17: