DataFrame.iloc
Purely integer-location based indexing for selection by position.
danfo.DataFrame.iloc(args)
args
Object
{
rows: Array, index of row position
columns: Array, index of position along columns
}
Examples
.iloc()
is primarily integer position based (from 0
to length-1
of the axis).
Allowed inputs are:
An integer, e.g.
5
.A list or array of integers, e.g.
[4, 3, 0]
.A string slice object with ints, e.g.
"1:7"
A boolean array.
Note: only the start index is included.
.iloc
will raiseIndexError
if a requested indexer is out-of-bounds.
Indexing specific rows by index and return all columns
If the row's index is specified and the columns are not, then it returns all columns and just the specified rows.
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
let sub_df = df.iloc({rows: [0,1,3]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ Apples │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ Pear │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Index by a slice of row and return all columns
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
let sub_df = df.iloc({rows: ["1:3"]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ Banana │ 30 │ 40 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Index by a slice of column and return all rows
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
df.print()
let sub_df = df.iloc({columns: ["1:"]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ Apples │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ Banana │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ Pear │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Shape: (4,2)
╔═══╤═══════════════════╤═══════════════════╗
║ │ Count │ Price ║
╟───┼───────────────────┼───────────────────╢
║ 0 │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────╢
║ 1 │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────╢
║ 2 │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────╢
║ 3 │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╝
Indexing both axis by the specified index
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
let sub_df = df.iloc({rows: [0,3], columns: [1,2]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ Apples │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ Banana │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ Pear │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
//after indexing
╔═══╤═══════════════════╤═══════════════════╗
║ │ Count │ Price ║
╟───┼───────────────────┼───────────────────╢
║ 0 │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────╢
║ 3 │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╝
Indexing both axis by slices
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
df.print()
let sub_df = df.iloc({rows: ["2:3"], columns: ["1:2"]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ Apples │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ Banana │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ Pear │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
╔═══╤═══════════════════╗
║ │ Count ║
╟───┼───────────────────╢
║ 2 │ 30 ║
╚═══╧═══════════════════╝
More default slicing behavior
If you specify a slice start position, iloc automatically returns all values after that position. For instance:
const dfd = require("danfojs-node")
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"],
"Count": [21, 5, 30, 10],
"Price": [200, 300, 40, 250] }
let df = new dfd.DataFrame(data)
df.print()
let sub_df = df.iloc({rows: ["2:"], columns: ["1:"]})
sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ Name │ Count │ Price ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ Apples │ 21 │ 200 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ Mango │ 5 │ 300 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ Banana │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ Pear │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Shape: (2,2)
╔═══╤═══════════════════╤═══════════════════╗
║ │ Count │ Price ║
╟───┼───────────────────┼───────────────────╢
║ 2 │ 30 │ 40 ║
╟───┼───────────────────┼───────────────────╢
║ 3 │ 10 │ 250 ║
╚═══╧═══════════════════╧═══════════════════╝
Last updated
Was this helpful?