Series.loc

Access a group of rows by label(s) or a boolean array.

danfo.Series.loc()

ParametersTypeDescriptionDefault

rows

Array, String

Array, string slice, index of row positions boolean mask to filter by.

Examples

.loc() is label position based (from 0 to length-1 of the row axis).

Allowed inputs are:

  • An integer, e.g. "r1".

  • A list or array of integers, e.g. ["a", "b", "d"].

  • A boolean mask. E.g [ true, false, false ]

  • A string slice object with ints, e.g. ['"a":"d"'], ["1:4"]

Note: only **** the start label is included, and the end label is ignored.

.loc will raise a ValueEror if a requested label is not found.

Indexing by specific row index

const dfd = require("danfojs-node")


const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
const index = ["a", "b", "c", "d", "e", "f", "g", "h"]
let s = new dfd.Series(data, { index })
s.print()

s.loc(["a", "g"]).print()
╔═══╤═════╗
║ a │ 12  ║
╟───┼─────╢
║ b │ 34  ║
╟───┼─────╢
║ c │ 2.2 ║
╟───┼─────╢
║ d │ 2   ║
╟───┼─────╢
║ e │ 30  ║
╟───┼─────╢
║ f │ 30  ║
╟───┼─────╢
║ g │ 2.1 ║
╟───┼─────╢
║ h │ 7   ║
╚═══╧═════╝

╔═══╤═════╗
║ a │ 12  ║
╟───┼─────╢
║ g │ 2.1 ║
╚═══╧═════╝

Index by a slice of row

The loc function also accepts string slices of the form [start: end], e.g [`"a":"e"`]. This will return all values from label positions a to e.

const dfd = require("danfojs-node")

const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
const index = ["a", "b", "c", "d", "e", "f", "g", "h"]
let s = new dfd.Series(data, { index })
s.print()

s.loc([`"a":"e"`]).print()
╔═══╤═════╗
║ a │ 12  ║
╟───┼─────╢
║ b │ 34  ║
╟───┼─────╢
║ c │ 2.2 ║
╟───┼─────╢
║ d │ 2   ║
╚═══╧═════╝

Note that when using loc. We expect you to pass labels in the correct format. That is, string labels must be explicitly quoted. For example, the following loc slice will throw an error: s.loc(["a:e"]).print() For the slice above to work, you must quote each slice, e.g: s.loc(["a":"e"]).print() Inner quotes are not needed for numeric indices!

By specifying a start index in a slice, all values after that index are returned.

const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
let s = new dfd.Series(data)

s.loc([`1:`]).print()
╔═══╤═════╗
║ 1 │ 34  ║
╟───┼─────╢
║ 2 │ 2.2 ║
╟───┼─────╢
║ 3 │ 2   ║
╟───┼─────╢
║ 4 │ 30  ║
╟───┼─────╢
║ 5 │ 30  ║
╟───┼─────╢
║ 6 │ 2.1 ║
╟───┼─────╢
║ 7 │ 7   ║
╚═══╧═════╝

Slice Series by boolean condition

const dfd = require("danfojs-node")


let s = new dfd.Series([12, 34, 2.2, 2, 30, 30, 2.1, 7])
s.loc(s.gt(20)).print()
╔═══╤════╗
║ 1 │ 34 ║
╟───┼────╢
║ 4 │ 30 ║
╟───┼────╢
║ 5 │ 30 ║
╚═══╧════╝

Last updated