danfo.DataFrame.loc(kwargs) [source]
Parameters | Type | Description | Default |
kwargs | Object | { rows: Array, labels of row index columns: Array, labels of column names } | |
Returns:
return DataFrame
.loc()
is primarily label based, but row index also accepts numeric slices
Allowed inputs are:
A single label, e.g. 5
or 'a'
, (note that 5
is interpreted as a label of the index, and not as an integer position along the index).
A list or array of labels, e.g. ['a', 'b', 'c']
.
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, {index: ["a", "b", "c", "d"]})df.print()let sub_df = df.loc({rows: ["a", "c"]})sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ Name │ Count │ Price ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ Apples │ 21 │ 200 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ b │ Mango │ 5 │ 300 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ c │ Banana │ 30 │ 40 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ d │ Pear │ 10 │ 250 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝Shape: (2,3)╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ Name │ Count │ Price ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ Apples │ 21 │ 200 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ c │ 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, {index: ["a", "b", "c", "d"]})df.print()let sub_df = df.loc({columns: ["Count", "Price"]})sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ Name │ Count │ Price ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ Apples │ 21 │ 200 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ b │ Mango │ 5 │ 300 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ c │ Banana │ 30 │ 40 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ d │ Pear │ 10 │ 250 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝//after slicing╔═══╤═══════════════════╤═══════════════════╗║ │ Count │ Price ║╟───┼───────────────────┼───────────────────╢║ a │ 21 │ 200 ║╟───┼───────────────────┼───────────────────╢║ b │ 5 │ 300 ║╟───┼───────────────────┼───────────────────╢║ c │ 30 │ 40 ║╟───┼───────────────────┼───────────────────╢║ d │ 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, { index: ["a", "b", "c", "d"] })df.print()let sub_df = df.loc({ rows: ["c","d"], columns: ["Name", "Price"] })sub_df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ Name │ Count │ Price ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ Apples │ 21 │ 200 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ b │ Mango │ 5 │ 300 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ c │ Banana │ 30 │ 40 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ d │ Pear │ 10 │ 250 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝//after slicing╔═══╤═══════════════════╤═══════════════════╗║ │ Name │ Price ║╟───┼───────────────────┼───────────────────╢║ c │ Banana │ 40 ║╟───┼───────────────────┼───────────────────╢║ d │ Pear │ 250 ║╚═══╧═══════════════════╧═══════════════════╝