Creating a DataFrame
Creates a DataFrame object from flat structure
new danfo.DataFrame(data, options)
Parameters
Type
Description
data
2D Array, 2D Tensor, JSON object.
Flat data structure to load into DataFrame
options
Object
Optional configuration object. Supported properties are:
index: Array of numeric or string names for subseting array. If not specified, indexes are auto-generated.
columns: Array of column names. If not specified, column names are auto generated.
dtypes: Array of data types for each the column. If not specified, dtypes are/is inferred.
config: General configuration object for extending or setting NDframe behavior. See full options here
In order to create a DataFrame, you new to call the new Keyword and pass in a flat data structure. In the following examples, we show you how to create DataFrames by specifying different config options.

Creating a DataFrame from 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 an array of array

Node
Browser
1
const dfd = require("danfojs-node")
2
3
let arr = [[12, 34, 2.2, 2], [30, 30, 2.1, 7]]
4
let df = new dfd.DataFrame(arr, {columns: ["A", "B", "C", "D"]})
5
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!
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ A │ B │ C │ D ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 0 │ 12 │ 34 │ 2.2 │ 2 ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 1 │ 30 │ 30 │ 2.1 │ 7 ║
7
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
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 from an object

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!

Creating a DataFrame and specifying index, dtypes, columns

You can create a DataFrame and specify options like index, column names, dtypes as well as configuration options like display, memory mode etc.
Note: Specifing dtypes, column names and index on DataFrame creation makes the process slightly faster.
Node
1
import { DataFrame } from "danfojs"
2
3
let data1 = [[1, 2.3, 3, 4, 5, "girl"], [30, 40.1, 39, 89, 78, "boy"]];
4
let index = ["a", "b"];
5
let columns = ["col1", "col2", "col3", "col4", "col5", "col6"]
6
let dtypes = ["int32", "float32", "int32", "int32", "int32", "string"]
7
8
let df = new DataFrame(data1, { index, columns, dtypes });
9
df.print()
Copied!
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ col1 │ col2 │ col3 │ col4 │ col5 │ col6 ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ a │ 1 │ 2.3 │ 3 │ 4 │ 5 │ girl ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ b │ 30 │ 40.1 │ 39 │ 89 │ 78 │ boy ║
7
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!

Creating a DataFrame and specifying memory mode

To use less space on DataFrame creation, you can set the low memory mode as demonstrated below:
1
import { DataFrame } from "danfojs"
2
3
let data1 = [[1, 2.3, 3, 4, 5, "girl"], [30, 40.1, 39, 89, 78, "boy"]];
4
5
let df = new DataFrame(data1, {
6
config: { lowMemoryMode: true }
7
});
8
df.print()
Copied!
Note: In low memory mode, less space is used by the DataFrame. The drawback is that some operations especially the ones involving column data become slightly slower.
For loading flat files like CSV, EXCEL and, JSON into DataFrames, see this page
Last modified 17d ago