danfo.DataFrame.iloc(kwargs) [source]
Parameters | Type | Description | Default |
kwargs | Object | { rows: Array, index of row position columns: Array, index of position along columns } | |
Returns:
return DataFrame
.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"
Note: only the start index is included.
.iloc
will raiseIndexError
if a requested indexer is out-of-bounds.
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 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
The iloc function also accepts string slices of the form [start: end], e.g "[1: 4]". This will return all values between index position 1 and 3. The end index is not included.
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 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
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 ║╚═══╧═══════════════════╧═══════════════════╝
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 slice╔═══╤═══════════════════╤═══════════════════╗║ │ Count │ Price ║╟───┼───────────────────┼───────────────────╢║ 0 │ 21 │ 200 ║╟───┼───────────────────┼───────────────────╢║ 3 │ 10 │ 250 ║╚═══╧═══════════════════╧═══════════════════╝
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 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝Shape: (1,1)╔═══╤═══════════════════╗║ │ Count ║╟───┼───────────────────╢║ 2 │ 30 ║╚═══╧═══════════════════╝
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 ║╚═══╧═══════════════════╧═══════════════════╝