Powered By GitBook
Series.loc
Access a group of rows by label(s) or a boolean array.
danfo.Series.loc() [source]
Parameters
Type
Description
Default
rows
Array, String
Array, string slice, index of row positions boolean mask to filter by.
Returns:
return Series

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

Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
5
const index = ["a", "b", "c", "d", "e", "f", "g", "h"]
6
let s = new dfd.Series(data, { index })
7
s.print()
8
9
s.loc(["a", "g"]).print()
10
Copied!
1
Copied!
Output
1
╔═══╤═════╗
2
║ a │ 12 ║
3
╟───┼─────╢
4
║ b │ 34 ║
5
╟───┼─────╢
6
║ c │ 2.2 ║
7
╟───┼─────╢
8
║ d │ 2 ║
9
╟───┼─────╢
10
║ e │ 30 ║
11
╟───┼─────╢
12
║ f │ 30 ║
13
╟───┼─────╢
14
║ g │ 2.1 ║
15
╟───┼─────╢
16
║ h │ 7 ║
17
╚═══╧═════╝
18
19
╔═══╤═════╗
20
║ a │ 12 ║
21
╟───┼─────╢
22
║ g │ 2.1 ║
23
╚═══╧═════╝
Copied!

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.
Node
Browser
1
const dfd = require("danfojs-node")
2
3
const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
4
const index = ["a", "b", "c", "d", "e", "f", "g", "h"]
5
let s = new dfd.Series(data, { index })
6
s.print()
7
8
s.loc([`"a":"e"`]).print()
Copied!
1
Copied!
Output
1
╔═══╤═════╗
2
║ a │ 12 ║
3
╟───┼─────╢
4
║ b │ 34 ║
5
╟───┼─────╢
6
║ c │ 2.2 ║
7
╟───┼─────╢
8
║ d │ 2 ║
9
╚═══╧═════╝
Copied!
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.

Node
Browser
1
const data = [12, 34, 2.2, 2, 30, 30, 2.1, 7]
2
let s = new dfd.Series(data)
3
4
s.loc([`1:`]).print()
Copied!
1
Copied!
Output
1
╔═══╤═════╗
2
║ 1 │ 34 ║
3
╟───┼─────╢
4
║ 2 │ 2.2 ║
5
╟───┼─────╢
6
║ 3 │ 2 ║
7
╟───┼─────╢
8
║ 4 │ 30 ║
9
╟───┼─────╢
10
║ 5 │ 30 ║
11
╟───┼─────╢
12
║ 6 │ 2.1 ║
13
╟───┼─────╢
14
║ 7 │ 7 ║
15
╚═══╧═════╝
16
Copied!

Slice Series by boolean condition

Node
1
const dfd = require("danfojs-node")
2
3
4
let s = new dfd.Series([12, 34, 2.2, 2, 30, 30, 2.1, 7])
5
s.loc(s.gt(20)).print()
Copied!
1
╔═══╤════╗
2
║ 1 │ 34 ║
3
╟───┼────╢
4
║ 4 │ 30 ║
5
╟───┼────╢
6
║ 5 │ 30 ║
7
╚═══╧════╝
Copied!
Last modified 17d ago