DataFrame.query
Query the DataFrame by the result of a logical comparison or boolean mask.
danfo.DataFrame.query(kwargs)
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
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
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
The query method also works on string columns.
Last updated
Was this helpful?