Danfo.js – Is it alternative of Python Pandas?
Data science is an upcoming need of markets. Python and R language are suitable for analysis and data science. If you’re a JS developer and want to touch this line. Then this tutorial is for you.
What is DanfoJS?
One minute, if you’ve Python pandas knowledge then this article will be beneficial for you too. DanfoJS is a powerful javascript data analysis toolkit. It is an open source, JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.

In addition, it 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.
What you can do with DanfoJS?
- Danfo.js is fast. It is built on Tensorflow.js, and supports 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 and delimited).
- 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 two ways to get danfo.js. We built an optimized and fast version for node.js and its available under the danfojs-node namespace. To install it via npm, you can do the following:
1 |
npm install danfojs-node |
You can also install and use it in the browsers by using the CDN below:
1 |
Implementation
DanfoJS can be run with Node or without NodeJS. So firstly take a small look of HTML based code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<!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/[email protected]/dist/index.min.js"></script> <title>Document</title> </head> <body> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> <script> dfd.read_csv("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.set_index({ key: "Date" }) //resets the index to Date column new_df.plot("div3").line({ columns: ["AAPL.Open", "AAPL.High"] }) //makes a timeseries plot }).catch(err => { console.log(err); }) </script> </body> </html> |
Output:
Example with Node.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
const dfd = require("danfojs-node") dfd.read_csv("https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv") .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.column_names); //prints the inferred dtypes of each column df.ctypes.print() //selecting a column by subsetting df['Name'].print() //drop columns by names cols_2_remove = ['Age', 'Pclass'] df_drop = df.drop({ columns: cols_2_remove, axis: 1 }) df_drop.print() //select columns by dtypes let str_cols = df_drop.select_dtypes(["string"]) let num_cols = df_drop.select_dtypes(["int32", "float32"]) str_cols.print() num_cols.print() //add new column to Dataframe let new_vals = df['Fare'].round().values df_drop.addColumn({ column: "fare_round", value: new_vals}) df_drop.print() df_drop['fare_round'].print(5) //prints the number of occurence each value in the column df_drop['Survived'].value_counts().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); }) |
Syntax is similar of Python pandas. You can easily do most of work which you can do with Pandas.
See the documentation for more.




