DataFrame.query

Query the DataFrame by the result of a logical comparison or boolean mask.

danfo.DataFrame.query(kwargs)

Parameters
Type
Description
Default

kwargs

Object

{

condition: A logical boolean mask,

column : str, name of the column

is: Logical operator, one of ">", "<", ">=", "<=", and. "=="

to: Int, Float, Str. Value to compare against,

inplace: boolean. true

false. Whether to perform operation to the original Object or create a new one.

}

Examples

Query a DataFrame using a boolean mask

Querying by a boolean condition is supported from v0.3.0 and above.

const dfd = require("danfojs-node")

let data = {
    "A": ["Ng", "Yu", "Mo", "Ng"],
    "B": [34, 4, 5, 6],
    "C": [20, 20, 30, 40]
}
let df = new dfd.DataFrame(data)
df.print()
let query_df = df.query(df["B"].gt(5))
query_df.print() //after query
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ Ng                │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1          │ Yu                │ 4                 │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2          │ Mo                │ 5                 │ 30                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 3          │ Ng                │ 6                 │ 40                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ Ng                │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 3          │ Ng                │ 6                 │ 40                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

It also supports condition chaining as long as the final boolean mask is the same length as the DataFrame rows. For example in the following code, we use multiple chaining conditions:

Query a DataFrame using logical operators

This is only supported in older versions. That is versions lower than v1.0.0

To query a DataFrame, you can specify the column to use, the logical operator (">", "<", ">=", "<=", and. "=="), and the value to compare against.

Query by a string column in a DataFrame

This is only supported in older versions. That is versions lower than v1.0.0

The query method also works on string columns.

Last updated

Was this helpful?