danfo.read_csv
Reads a comma-separated values (CSV) file into DataFrame. Also supports the reading of CSV files in chunks.
danfo.read_csv(source, configs) [source]
Parameters
Type
Description
Default
source
File object, File path, URL
Any valid string path is acceptable. The string could be a URL or a valid local file path.
A browser input file object is also supported.
configs:
object, optional
Supports all Papaparse config parameters. See https://www.papaparse.com/docs#config.
{
dynamicTyping: true,
header: true
}
Returns:
** **Promise. Resolves to DataFrame
The read_csv method can read a CSV file from a local disk, or over the internet (URL). Reading of local files is only supported in Nodejs, while reading of input file objects is only supported in the browser.

Reading files from local disk

By specifying a valid file path, you can load CSV files from local disk:
Node.js
1
const dfd = require("danfojs-node")
2
3
dfd.read_csv("./user_names.csv") //assumes file is in CWD
4
.then(df => {
5
6
df.head().print()
7
8
}).catch(err=>{
9
console.log(err);
10
})
Copied!

Reading files from a URL

By specifying a valid URL, you can load CSV files from any location into Danfo**'**s data structure:
Node.js
Browser
1
const dfd = require("danfojs-node")
2
3
dfd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv") //assumes file is in CWD
4
.then(df => {
5
6
df.head().print()
7
8
}).catch(err=>{
9
console.log(err);
10
})
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.plot.ly/plotly-2.2.0.min.js"></script>
8
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script>
9
<title>Document</title>
10
</head>
11
12
<body>
13
14
<div id="plot_div"></div>
15
<script>
16
17
dfd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
18
.then(df => {
19
20
//do something like display descriptive statistics
21
df.describe().print()
22
23
}).catch(err => {
24
console.log(err);
25
})
26
27
</script>
28
</body>
29
30
</html>
Copied!

Reading an input file object in the browser

By specifying a valid file object, you can load CSV files in the browser in DataFrames/Series
Browser
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
<title>Document</title>
9
</head>
10
11
<body>
12
<input type="file" id="file" name="file">
13
<script>
14
15
inputFile.addEventListener("change", async () => {
16
const csvFile = inputFile.files[0]
17
dfd.read_csv(csvFile).then((df) => {
18
df.print()
19
})
20
})
21
22
</script>
23
</body>
24
25
</html>
Copied!
Last modified 1mo ago