Getting Started
Installation guides for Node and Browser based environments, including a quick 10 minute walk through of danfo.js

Installation

There are three ways to install and use Danfo.js in your application
For Nodejs applications, you can install the danfojs-node version via package managers like yarn and npm:
1
npm install danfojs-node
2
3
or
4
5
yarn add danfojs-node
Copied!
For client-side applications built with frameworks like React, Vue, Next.js, etc, you can install the danfojs version:
1
npm install danfojs
2
3
or
4
5
yarn add danfojs
Copied!
For use directly in HTML files, you can add the latest script tag from JsDelivr:
1
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script>
Copied!
To play with Danfo.js in a Notebook-like environment, see Dnotebooks here

10 minutes to danfo.js

This is a short introduction to Danfo.js, and its flow is adapted from the official 10 minutes to Pandas
We will show you how to use danfo.js in both browser environments and Node.js environments. Most functions except plotting which require a DOM work the same way in both environments.
Node
Browser
1
const dfd = require("danfojs-node")
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script>
8
</head>
9
10
<body>
11
12
<script>
13
14
//danfo is exposed on dfd namespace
15
s = new dfd.Series([1,2,3,4,5])
16
17
</script>
18
19
</body>
20
21
</html>
Copied!

Creating a DataFrame/Series

You can create a Series by passing a list of values, letting Danfo.js create a default integer index:
Node
Browser
1
s = new dfd.Series([1, 3, 5, undefined, 6, 8])
2
s.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
8
</head>
9
10
<body>
11
12
<script>
13
14
s = new dfd.Series([1, 3, 5, undefined, 6, 8])
15
s.print()
16
17
</script>
18
19
</body>
20
21
</html>
Copied!
1
//output
2
╔═══╤══════════════════════╗
3
║ │ 0 ║
4
╟───┼──────────────────────╢
5
║ 0 │ 1 ║
6
╟───┼──────────────────────╢
7
║ 1 │ 3 ║
8
╟───┼──────────────────────╢
9
║ 2 │ 5 ║
10
╟───┼──────────────────────╢
11
║ 3 │ undefined ║
12
╟───┼──────────────────────╢
13
║ 4 │ 6 ║
14
╟───┼──────────────────────╢
15
║ 5 │ 8 ║
16
╚═══╧══════════════════════╝
Copied!
Creating a Series from a tensor
Node
Browser
1
const dfd = require("danfojs-node")
2
const tf = require("@tensorflow/tfjs-node")
3
4
5
let tensor_arr = tf.tensor([12,34,56,2])
6
let s = new dfd.Series(tensor_arr)
7
s.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script>
8
9
<title>Document</title>
10
</head>
11
12
<body>
13
14
<script>
15
const tf = dfd.tf //get tensorflow lib from danfo
16
let tensor_arr = tf.tensor([12,34,56,2])
17
let s = new dfd.Series(tensor_arr)
18
s.print()
19
20
</script>
21
22
</body>
23
24
</html>
Copied!
1
╔═══╤════╗
2
║ 0 │ 12 ║
3
╟───┼────╢
4
║ 1 │ 34 ║
5
╟───┼────╢
6
║ 2 │ 56 ║
7
╟───┼────╢
8
║ 3 │ 2 ║
9
╚═══╧════╝
Copied!
Creating a DataFrame by passing a JSON object:
Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
5
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
6
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
7
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
8
9
df = new dfd.DataFrame(json_data)
10
df.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
16
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
17
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
18
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
19
20
df = new dfd.DataFrame(json_data)
21
df.print()
22
23
</script>
24
</body>
25
26
</html>
Copied!
Creating a DataFrame from a 2D tensor
Node
Browser
1
const dfd = require("danfojs-node")
2
const tf = require("@tensorflow/tfjs-node")
3
4
5
let tensor_arr = tf.tensor2d([[12, 34, 2.2, 2], [30, 30, 2.1, 7]])
6
let df = new dfd.DataFrame(tensor_arr, {columns: ["A", "B", "C", "D"]})
7
df.print()
8
df.ctypes.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
16
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
17
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
18
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
19
20
df = new dfd.DataFrame(json_data)
21
df.print()
22
23
</script>
24
</body>
25
26
</html>
Copied!
1
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ A │ B │ C │ D ║
3
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 0 │ 12 │ 34 │ 2.20000004768... │ 2 ║
5
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 1 │ 30 │ 30 │ 2.09999990463... │ 7 ║
7
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
8
9
╔═══╤══════════════════════╗
10
║ │ 0 ║
11
╟───┼──────────────────────╢
12
║ A │ int32 ║
13
╟───┼──────────────────────╢
14
║ B │ int32 ║
15
╟───┼──────────────────────╢
16
║ C │ float32 ║
17
╟───┼──────────────────────╢
18
║ D │ int32 ║
19
╚═══╧══════════════════════╝
Copied!
Creating a DataFrame by passing a dictionary of objects with the same length
Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
dates = new dfd.date_range({ start: '2017-01-01', end: "2020-01-01", period: 4, freq: "Y" })
5
6
console.log(dates);
7
8
obj_data = {'A': dates,
9
'B': ["bval1", "bval2", "bval3", "bval4"],
10
'C': [10, 20, 30, 40],
11
'D': [1.2, 3.45, 60.1, 45],
12
'E': ["test", "train", "test", "train"]
13
}
14
15
df = new dfd.DataFrame(obj_data)
16
df.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
dates = new dfd.date_range({ start: '2017-01-01', end: "2020-01-01", period: 4, freq: "Y" })
16
17
console.log(dates);
18
19
obj_data = {'A': dates,
20
'B': ["bval1", "bval2", "bval3", "bval4"],
21
'C': [10, 20, 30, 40],
22
'D': [1.2, 3.45, 60.1, 45],
23
'E': ["test", "train", "test", "train"]
24
}
25
26
df = new dfd.DataFrame(obj_data)
27
df.print()
28
29
</script>
30
</body>
31
32
</html>
Copied!
1
//output in console
2
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
3
║ │ A │ B │ C │ D │ E ║
4
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
5
║ 0 │ 1/1/2017, 1:0... │ bval1 │ 10 │ 1.2 │ test ║
6
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
7
║ 1 │ 1/1/2018, 1:0... │ bval2 │ 20 │ 3.45 │ train ║
8
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
9
║ 2 │ 1/1/2019, 1:0... │ bval3 │ 30 │ 60.1 │ test ║
10
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
11
║ 3 │ 1/1/2020, 1:0... │ bval4 │ 40 │ 45 │ train ║
12
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!
The columns of the resulting DataFrame have different dtypes.
1
df.ctypes.print()
Copied!
1
//output
2
╔═══╤═════════╗
3
║ A │ string ║
4
╟───┼─────────╢
5
║ B │ string ║
6
╟───┼─────────╢
7
║ C │ int32 ║
8
╟───┼─────────╢
9
║ D │ float32 ║
10
╟───┼─────────╢
11
║ E │ string ║
12
╚═══╧═════════╝
Copied!
Creating a DataFrame by passing an array of arrays. Index and column labels are automatically generated for you.
Node
Browser
1
const dfd = require("danfojs-node")
2
3
arr_data = [["bval1", 10, 1.2, "test"],
4
["bval2", 20, 3.45, "train"],
5
["bval3", 30, 60.1, "train"],
6
["bval4", 35, 3.2, "test"]]
7
8
df = new dfd.DataFrame(arr_data)
9
df.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
arr_data = [["bval1", 10, 1.2, "test"],
16
["bval2", 20, 3.45, "train"],
17
["bval3", 30, 60.1, "train"],
18
["bval4", 35, 3.2, "test"]]
19
20
df = new dfd.DataFrame(arr_data)
21
df.print()
22
23
</script>
24
</body>
25
26
</html>
Copied!
1
//output in console
2
3
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
4
║ │ 0 │ 1 │ 2 │ 3 ║
5
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 0 │ bval1 │ 10 │ 1.2 │ test ║
7
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
8
║ 1 │ bval2 │ 20 │ 3.45 │ train ║
9
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
10
║ 2 │ bval3 │ 30 │ 60.1 │ train ║
11
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
12
║ 3 │ bval4 │ 35 │ 3.2 │ test ║
13
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!

Viewing data

Here is how to view the top and bottom rows of the frame above:
1
df.head(2).print()
2
df.tail(2).print()
Copied!
1
//output from head
2
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
3
║ │ 0 │ 1 │ 2 │ 3 ║
4
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
5
║ 0 │ bval1 │ 10 │ 1.2 │ test ║
6
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
7
║ 1 │ bval2 │ 20 │ 3.45 │ train ║
8
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
9
10
11
//output from tail
12
13
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
14
║ │ 0 │ 1 │ 2 │ 3 ║
15
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
16
║ 2 │ bval3 │ 30 │ 60.1 │ train ║
17
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
18
║ 3 │ bval4 │ 35 │ 3.2 │ test ║
19
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!
Display the index, columns:
JavaScript
Browser
1
const dfd = require("danfojs-node")
2
3
dates = new dfd.date_range({ start: '2017-01-01', end: "2020-01-01", period: 4, freq: "Y" })
4
5
6
obj_data = {'A': dates,
7
'B': ["bval1", "bval2", "bval3", "bval4"],
8
'C': [10, 20, 30, 40],
9
'D': [1.2, 3.45, 60.1, 45],
10
'E': ["test", "train", "test", "train"]
11
}
12
13
df = new dfd.DataFrame(obj_data)
14
15
console.log(df.index);
16
console.log(df.columns);
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
dates = new dfd.date_range({ start: '2017-01-01', end: "2020-01-01", period: 4, freq: "Y" })
16
17
18
obj_data = {'A': dates,
19
'B': ["bval1", "bval2", "bval3", "bval4"],
20
'C': [10, 20, 30, 40],
21
'D': [1.2, 3.45, 60.1, 45],
22
'E': ["test", "train", "test", "train"]
23
}
24
25
df = new dfd.DataFrame(obj_data)
26
27
console.log(df.index);
28
console.log(df.columns);
29
30
</script>
31
</body>
32
33
</html>
Copied!
1
//output
2
[ 0, 1, 2, 3 ]
3
[ 'A', 'B', 'C', 'D', 'E' ]
Copied!
DataFrame.tensor returns a Tensorflow tensor representation of the underlying data. Note that Tensorflow tensors have one dtype for the entire array, while danfo DataFrames have one dtype per column.
For df, our DataFrame of all floating-point values, DataFrame.tensoris fast and doesn’t require copying data.
Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
5
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
6
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
7
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
8
9
df = new dfd.DataFrame(json_data)
10
11
console.log(df.tensor);
12
//or
13
df.tensor.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
16
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
17
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
18
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
19
20
df = new dfd.DataFrame(json_data)
21
22
console.log(df.tensor);
23
//or
24
df.tensor.print()
25
26
</script>
27
</body>
28
29
</html>
Copied!
1
//output
2
3
Tensor {
4
kept: false,
5
isDisposedInternal: false,
6
shape: [ 4, 4 ],
7
dtype: 'float32',
8
size: 16,
9
strides: [ 4 ],
10
dataId: {},
11
id: 0,
12
rankType: '2'
13
}
14
15
Tensor
16
[[0.4612, 4.2828302, -1.5089999, -1.1352 ],
17
[0.5112, -0.22863 , -3.39059 , 1.1632 ],
18
[0.6911, -0.82863 , -1.5059 , 2.1352 ],
19
[0.4692, -1.28863 , 4.5058999 , 4.1631999]]
Copied!
Note
DataFrame.tensor does not include the index or column labels in the output.
describe() shows a quick statistic summary of your data:
Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
5
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
6
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
7
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
8
9
df = new dfd.DataFrame(json_data)
10
11
df.describe().print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
16
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
17
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
18
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
19
20
df = new dfd.DataFrame(json_data)
21
22
df.describe().print()
23
24
</script>
25
</body>
26
27
</html>
Copied!
1
//output in console
2
3
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
4
║ │ A │ B │ C │ D ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ count │ 4 │ 4 │ 4 │ 4 ║
7
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
8
║ mean │ 0.533175 │ 0.4842349999999… │ -0.474897500000… │ 1.5816 ║
9
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
10
║ std │ 0.1075428712963… │ 2.5693167249095… │ 3.4371471031498… │ 2.2005448052698… ║
11
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
12
║ min │ 0.4612 │ -1.28863 │ -3.39059 │ -1.1352 ║
13
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
14
║ median │ 0.4901999999999… │ -0.528629999999… │ -1.50745 │ 1.6492 ║
15
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
16
║ max │ 0.6911 │ 4.28283 │ 4.5059 │ 4.1632 ║
17
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
18
║ variance │ 0.0115654691666… │ 6.6013884328999… │ 11.813980208691… │ 4.84239744 ║
19
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!
Sorting by values (Defaults to ascending):
Node
Browser
1
const dfd = require("danfojs")
2
3
let data = {"A": [-20, 30, 47.3, NaN],
4
"B": [34, -4, 5, 6] ,
5
"C": [20, 2, 3, 30] }
6
7
8
let df = new dfd.DataFrame(data)
9
df.sort_values({by: "C", inplace: true})
10
df.print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
let data = {"A": [-20, 30, 47.3, NaN],
16
"B": [34, -4, 5, 6] ,
17
"C": [20, 2, 3, 30] }
18
19
20
let df = new dfd.DataFrame(data)
21
df.sort_values({by: "C", inplace: true})
22
df.print()
23
24
</script>
25
</body>
26
27
</html>
Copied!
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ A │ B │ C ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 1 │ 30 │ -4 │ 2 ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 2 │ 47.3 │ 5 │ 3 ║
7
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
8
║ 0 │ -20 │ 34 │ 20 ║
9
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
10
║ 3 │ NaN │ 6 │ 30 ║
11
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!

Selection

Getting

Selecting a single column, which yields a Series, equivalent to df.A:
Node
Browser
1
const dfd = require("danfojs-node")
2
3
4
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
5
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
6
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
7
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
8
9
df = new dfd.DataFrame(json_data)
10
11
df['A'].print()
Copied!
1
<!DOCTYPE html>
2
<html lang="en">
3
4
<head>
5
<meta charset="UTF-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<!--danfojs CDN -->
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
9
</head>
10
11
<body>
12
13
<script>
14
15
16
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
17
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
18
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
19
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
20
21
df = new dfd.DataFrame(json_data)
22
23
df['A'].print()
24
25
</script>
26
</body>
27
28
</html>
Copied!
1
//output
2
╔═══╤══════════════════════╗
3
║ │ A ║
4
╟───┼──────────────────────╢
5
║ 0 │ 0.4612 ║
6
╟───┼──────────────────────╢
7
║ 1 │ 0.5112 ║
8
╟───┼──────────────────────╢
9
║ 2 │ 0.6911 ║
10
╟───┼──────────────────────╢
11
║ 3 │ 0.4692 ║
12
╚═══╧══════════════════════╝
Copied!

Selection by label

For getting a cross-section using a label:
1
const dfd = require("danfojs")
2
3
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"] ,
4
"Count": [21, 5, 30, 10] ,
5
"Price": [200, 300, 40, 250] }
6
7
let df = new dfd.DataFrame(data, {index: ["a", "b", "c", "d"]})
8
df.print()
9
10
let sub_df = df.loc({rows: ["a", "c"]})
11
sub_df.print()
Copied!
1
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ Name │ Count │ Price ║
3
╟───┼───────────────────┼───────────────────┼───────────────────╢
4
║ a │ Apples │ 21 │ 200 ║
5
╟───┼───────────────────┼───────────────────┼───────────────────╢
6
║ b │ Mango │ 5 │ 300 ║
7
╟───┼───────────────────┼───────────────────┼───────────────────╢
8
║ c │ Banana │ 30 │ 40 ║
9
╟───┼───────────────────┼───────────────────┼───────────────────╢
10
║ d │ Pear │ 10 │ 250 ║
11
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
12
13
14
Shape: (2,3)
15
16
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
17
║ │ Name │ Count │ Price ║
18
╟───┼───────────────────┼───────────────────┼───────────────────╢
19
║ a │ Apples │ 21 │ 200 ║
20
╟───┼───────────────────┼───────────────────┼───────────────────╢
21
║ c │ Banana │ 30 │ 40 ║
22
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!
Selecting on a multi-axis by label:
1
const dfd = require("danfojs-node")
2
3
let data = { "Name": ["Apples", "Mango", "Banana", "Pear"] ,
4
"Count": [21, 5, 30, 10],
5
"Price": [200, 300, 40, 250] }
6
7
let df = new dfd.DataFrame(data)
8
df.print()
9
10
let sub_df = df.loc({ rows: [0,1], columns: ["Name", "Price"] })
11
sub_df.print()
Copied!
1
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ Name │ Count │ Price ║
3
╟───┼───────────────────┼───────────────────┼───────────────────╢