DataFrame.sample
Return a random sample of rows from DataFrame.
danfo.DataFrame.sample(num, seed) [source]
Parameters
Type
Description
Default
num
Int
The number of rows to return. Defaults to -1, which shuffles and return all rows.
-1
seed
int
An integer specifying the random seed that will be used to create the distribution. Ensures reproducibility of generated samples.
1
Returns:
return {Promies} resolves to DataFrame

Sample a DataFrame randomly

Node
Browser
1
const dfd = require("danfojs-node")
2
3
async function load_data() {
4
let data = {
5
Name: ["Apples", "Mango", "Banana", "Pear"],
6
Count: [21, 5, 30, 10],
7
Price: [200, 300, 40, 250],
8
};
9
10
let df = new dfd.DataFrame(data);
11
let s_df = await df.sample(2);
12
s_df.print();
13
14
}
15
16
load_data()
Copied!
1
Copied!
Output
1
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ Name │ Count │ Price ║
3
╟───┼───────────────────┼───────────────────┼───────────────────╢
4
║ 0 │ Apples │ 21 │ 200 ║
5
╟───┼───────────────────┼───────────────────┼───────────────────╢
6
║ 2 │ Banana │ 30 │ 40 ║
7
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Copied!

Sample a DataFrame randomly with seed

By setting seed when using sample, you can ensure that the random sampling is reproducible.
Node
Browser
1
const dfd = require("danfojs-node")
2
3
async function load_data() {
4
let data = {
5
Name: ["Apples", "Mango", "Banana", "Pear"],
6
Count: [21, 5, 30, 10],
7
Price: [200, 300, 40, 250],
8
};
9
10
let df = new dfd.DataFrame(data);
11
let s_df = await df.sample(3, { seed: 2 });
12
s_df.print();
13
14
}
15
16
load_data()
Copied!
1
Copied!
Output
1
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
2
║ │ Name │ Count │ Price ║
3
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
4
║ 1 │ Mango │ 5 │ 300 ║
5
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
6
║ 2 │ Banana │ 30 │ 40 ║
7
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
8
║ 0 │ Apples │ 21 │ 200 ║
9
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
10
11
Copied!
Last modified 17d ago