Powered By GitBook
Creating a Series
new danfo.Series(data, options)
Parameters
Type
Description
data
1D Array, 1D 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.
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 Series, you need to call the new Keyword and pass a flat data structure. In the following examples, we show you how to create a Series by specifying different config options.

Creating a Series from an object:

Node
Browser
1
const dfd = require("danfojs-node")
2
3
obj_data = { 'B': ["bval1", "bval2", "bval3", "bval4"] }
4
df = new dfd.Series(obj_data)
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
0 │ bval1 ║
3
╟───┼───────╢
4
1 │ bval2 ║
5
╟───┼───────╢
6
2 │ bval3 ║
7
╟───┼───────╢
8
3 │ bval4 ║
9
╚═══╧═══════╝
Copied!

Creating a Series from an array

Node
Browser
1
const dfd = require("danfojs-node")
2
3
obj_data = ["bval1", "bval2", "bval3", "bval4"]
4
df = new dfd.Series(obj_data)
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
║ 0 │ bval1 ║
3
╟───┼───────╢
4
║ 1 │ bval2 ║
5
╟───┼───────╢
6
║ 2 │ bval3 ║
7
╟───┼───────╢
8
║ 3 │ bval4 ║
9
╚═══╧═══════╝
Copied!

Creating a Series and specifying index and dtypes

You can create a Series and specify options like index, dtypes, as well as configuration options for display, and memory mode etc.
Note: Specifing dtypes, and index on Series creation makes the process slightly faster.
Node
1
import { Series } from "danfojs"
2
3
let data1 = [1, 2, 3, 4, 5];
4
let index = ["a", "b", "c", "d", "e"];
5
let dtypes = ["int32",]
6
7
let df = new Series(data1, { index, dtypes });
8
df.print()
Copied!
1
╔═══╤═══╗
2
║ a │ 1 ║
3
╟───┼───╢
4
║ b │ 2 ║
5
╟───┼───╢
6
║ c │ 3 ║
7
╟───┼───╢
8
║ d │ 4 ║
9
╟───┼───╢
10
║ e │ 5 ║
11
╚═══╧═══╝
Copied!

Creating a Series and specifying memory mode

To use less space on Series creation, you can set the low memory mode as demonstrated below:
1
import { Series } from "danfojs"
2
3
let data1 = [1, 2.3, 3, 4, 5, "girl"];
4
5
let df = new Series(data1, {
6
config: { lowMemoryMode: true }
7
});
8
df.print()
Copied!
Note: In low memory mode, less space is used by the Series.
Last modified 17d ago