danfojs
Danfo.js is an open source, JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.
Top Related Projects
✨ Standard library for JavaScript and Node.js. ✨
The JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
Apache Arrow is a multi-language toolbox for accelerated data interchange and in-memory processing
Quick Overview
Danfo.js is a powerful open-source JavaScript library for data manipulation and analysis. It provides a pandas-like API for working with structured data in JavaScript and TypeScript, making it easier for developers to perform complex data operations in both browser and Node.js environments.
Pros
- Familiar pandas-like API for JavaScript developers
- Supports both browser and Node.js environments
- Offers a wide range of data manipulation and analysis functions
- Integrates well with visualization libraries like Plotly.js
Cons
- Performance may be slower compared to native JavaScript operations for large datasets
- Limited ecosystem compared to more established data analysis libraries
- Documentation could be more comprehensive for advanced use cases
- Steeper learning curve for developers not familiar with pandas
Code Examples
- Creating a DataFrame:
import { DataFrame } from "danfojs-node"
const data = {
"Name": ["John", "Jane", "Mike"],
"Age": [28, 34, 22],
"City": ["New York", "London", "Paris"]
}
const df = new DataFrame(data)
console.log(df.head())
- Filtering and sorting data:
// Filter rows where Age > 25 and sort by Age in descending order
const filtered = df.query("Age > 25").sortValues("Age", { ascending: false })
console.log(filtered)
- Performing group operations:
// Group by City and calculate mean Age
const grouped = df.groupby(["City"]).mean()
console.log(grouped)
Getting Started
To get started with Danfo.js, follow these steps:
-
Install the library using npm:
npm install danfojs-node
-
Import the necessary modules in your JavaScript file:
import { DataFrame, Series } from "danfojs-node"
-
Create a DataFrame and start manipulating your data:
const df = new DataFrame({ "A": [1, 2, 3], "B": [4, 5, 6] }) console.log(df.describe().print())
For more detailed information and advanced usage, refer to the official Danfo.js documentation.
Competitor Comparisons
✨ Standard library for JavaScript and Node.js. ✨
Pros of stdlib
- Comprehensive library with a wide range of mathematical and statistical functions
- Well-documented and actively maintained
- Supports both Node.js and browser environments
Cons of stdlib
- Larger package size due to its extensive functionality
- Steeper learning curve for beginners
- May be overkill for projects that only need basic data manipulation
Code Comparison
danfojs:
const dfd = require("danfojs-node")
let df = new dfd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
let mean = df.mean()
console.log(mean)
stdlib:
const stdlib = require('@stdlib/stdlib');
const arr = [1, 2, 3, 4, 5, 6];
const mean = stdlib.stats.mean(arr);
console.log(mean);
Summary
While stdlib offers a more comprehensive set of mathematical and statistical functions, danfojs focuses specifically on data manipulation and analysis, similar to pandas in Python. stdlib is better suited for projects requiring advanced mathematical operations, while danfojs may be more appropriate for data-centric applications. The choice between the two depends on the specific needs of your project and your familiarity with each library's syntax and structure.
The JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
Pros of Data-Forge-ts
- Written in TypeScript, providing better type safety and developer experience
- More comprehensive data manipulation capabilities, including advanced filtering and grouping
- Supports both Node.js and browser environments
Cons of Data-Forge-ts
- Smaller community and fewer contributors compared to Danfojs
- Less focus on machine learning and statistical operations
- Documentation may be less extensive and user-friendly
Code Comparison
Data-Forge-ts:
import { DataFrame } from 'data-forge';
const df = new DataFrame({
columnNames: ["A", "B", "C"],
rows: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
});
const filtered = df.where(row => row.A > 3);
Danfojs:
import dfd from "danfojs-node"
const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
{ columns: ["A", "B", "C"] })
const filtered = df.query({ column: "A", is: ">", to: 3 })
Both libraries offer similar functionality for creating and manipulating dataframes, but with slightly different syntax and approaches. Data-Forge-ts leverages TypeScript's type system, while Danfojs focuses on providing a pandas-like API for JavaScript developers.
Apache Arrow is a multi-language toolbox for accelerated data interchange and in-memory processing
Pros of Arrow
- Broader language support (C++, Python, R, etc.) beyond JavaScript
- More mature project with larger community and ecosystem
- Optimized for high-performance data processing and analytics
Cons of Arrow
- Steeper learning curve due to its lower-level nature
- Less focused on data manipulation and analysis in JavaScript specifically
- Requires more setup and configuration for JavaScript projects
Code Comparison
Arrow (JavaScript):
const arrow = require('apache-arrow');
const table = arrow.Table.from([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
]);
Danfojs:
const dfd = require('danfojs-node');
const df = new dfd.DataFrame([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
]);
Summary
Arrow is a cross-language development platform for in-memory data, offering high performance and interoperability. Danfojs is specifically designed for data manipulation and analysis in JavaScript, providing a more accessible API for web developers. While Arrow excels in performance and multi-language support, Danfojs offers a more straightforward approach for JavaScript-centric data projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Danfojs: powerful javascript data analysis toolkit
What is it?
Danfo.js is a javascript package that provides fast, flexible, and expressive data structures designed to make working with "relational" or "labeled" data both easy and intuitive. It is heavily inspired by Pandas library, and provides a similar API. This means that users familiar with Pandas, can easily pick up danfo.js.
Main Features
- Danfo.js is fast and supports Tensorflow.js tensors out of the box. This means you can convert Danfo data structure to Tensors.
- Easy handling of missing-data (represented as
NaN
) in floating point as well as non-floating point data - Size mutability: columns can be inserted/deleted from DataFrame
- Automatic and explicit alignment: objects can
be explicitly aligned to a set of labels, or the user can simply
ignore the labels and let
Series
,DataFrame
, etc. automatically align the data for you in computations - Powerful, flexible groupby functionality to perform split-apply-combine operations on data sets, for both aggregating and transforming data
- Make it easy to convert Arrays, JSONs, List or Objects, Tensors and differently-indexed data structures into DataFrame objects
- Intelligent label-based slicing, fancy indexing, and querying of large data sets
- Intuitive merging and joining data sets
- Robust IO tools for loading data from flat-files (CSV, Json, Excel).
- Powerful, flexible and intutive API for plotting DataFrames and Series interactively.
- Timeseries-specific functionality: date range generation and date and time properties.
- Robust data preprocessing functions like OneHotEncoders, LabelEncoders, and scalers like StandardScaler and MinMaxScaler are supported on DataFrame and Series
Installation
There are three ways to install and use Danfo.js in your application
- For Nodejs applications, you can install the danfojs-node version via package managers like yarn and/or npm:
npm install danfojs-node
or
yarn add danfojs-node
For client-side applications built with frameworks like React, Vue, Next.js, etc, you can install the danfojs version:
npm install danfojs
or
yarn add danfojs
For use directly in HTML files, you can add the latest script tag from JsDelivr to your HTML file:
<script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
See all available versions here
Quick Examples
- Danfojs with HTML and vanilla JavaScript on CodePen
- Danfojs with React on Code Sandbox
- Danfojs on ObservableHq
- Danfojs in Nodejs on Replit
Example Usage in the Browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
<title>Document</title>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<script>
dfd.readCSV("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
.then(df => {
df['AAPL.Open'].plot("div1").box() //makes a box plot
df.plot("div2").table() //display csv as table
new_df = df.setIndex({ column: "Date", drop: true }); //resets the index to Date column
new_df.head().print() //
new_df.plot("div3").line({
config: {
columns: ["AAPL.Open", "AAPL.High"]
}
}) //makes a timeseries plot
}).catch(err => {
console.log(err);
})
</script>
</body>
</html>
Output in Browser:
Example usage in Nodejs
const dfd = require("danfojs-node");
const file_url =
"https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv";
dfd
.readCSV(file_url)
.then((df) => {
//prints the first five columns
df.head().print();
// Calculate descriptive statistics for all numerical columns
df.describe().print();
//prints the shape of the data
console.log(df.shape);
//prints all column names
console.log(df.columns);
// //prints the inferred dtypes of each column
df.ctypes.print();
//selecting a column by subsetting
df["Name"].print();
//drop columns by names
let cols_2_remove = ["Age", "Pclass"];
let df_drop = df.drop({ columns: cols_2_remove, axis: 1 });
df_drop.print();
//select columns by dtypes
let str_cols = df_drop.selectDtypes(["string"]);
let num_cols = df_drop.selectDtypes(["int32", "float32"]);
str_cols.print();
num_cols.print();
//add new column to Dataframe
let new_vals = df["Fare"].round(1);
df_drop.addColumn("fare_round", new_vals, { inplace: true });
df_drop.print();
df_drop["fare_round"].round(2).print(5);
//prints the number of occurence each value in the column
df_drop["Survived"].valueCounts().print();
//print the last ten elementa of a DataFrame
df_drop.tail(10).print();
//prints the number of missing values in a DataFrame
df_drop.isNa().sum().print();
})
.catch((err) => {
console.log(err);
});
Output in Node Console:
Notebook support
- VsCode nodejs notebook extension now supports Danfo.js. See guide here
- ObservableHQ Notebooks. See example notebook here
See the Official Getting Started Guide
Documentation
The official documentation can be found here
Danfo.js Official Book
We published a book titled "Building Data Driven Applications with Danfo.js". Read more about it here
Discussion and Development
Development discussions take place here.
Contributing to Danfo
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome. A detailed overview on how to contribute can be found in the contributing guide.
Licence MIT
Created by Rising Odegua and Stephen Oni
Top Related Projects
✨ Standard library for JavaScript and Node.js. ✨
The JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
Apache Arrow is a multi-language toolbox for accelerated data interchange and in-memory processing
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot