Creating a DataFrame
Creates a DataFrame object from flat structure
danfo.DataFrame(data, options)
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 need 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:
DataFrame from a JSON object:const dfd = require("danfojs-node")
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
df = new dfd.DataFrame(json_data)
df.print()<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--danfojs CDN -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
</head>
<body>
<script>
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
df = new dfd.DataFrame(json_data)
df.print()
</script>
</body>
</html>Creating a DataFrame from an array of array
DataFrame from an array of arrayconst dfd = require("danfojs-node")
let arr = [[12, 34, 2.2, 2], [30, 30, 2.1, 7]]
let df = new dfd.DataFrame(arr, {columns: ["A", "B", "C", "D"]})
df.print()<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--danfojs CDN -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script> <title>Document</title>
</head>
<body>
<script>
json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
{ A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
{ A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
{ A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }]
df = new dfd.DataFrame(json_data)
df.print()
</script>
</body>
</html>Creating a DataFrame from a 2D tensor
DataFrame from a 2D tensorCreating a DataFrame from an object
DataFrame from an objectCreating a DataFrame and specifying index, dtypes, columns
DataFrame and specifying index, dtypes, columnsYou 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.
Creating a DataFrame and specifying memory mode
DataFrame and specifying memory modeTo use less space on DataFrame creation, you can set the low memory mode as demonstrated below:
For loading flat files like CSV, EXCEL and, JSON into DataFrames, see this page
Last updated
Was this helpful?