Danfo

high performance, intuitive, and easy to use data structures for manipulati...

README





Danfojs: powerful javascript data analysis toolkit

Node.js CI undefined Coverage Status
Twitter
Patreon donate button

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:

  1. ``` sh
  2. npm install danfojs-node

  3. or

  4. yarn add danfojs-node
  5. ```
For client-side applications built with frameworks like React, Vue, Next.js, etc, you can install the [__danfojs__]() version:

  1. ``` sh
  2. npm install danfojs

  3. or

  4. yarn add danfojs
  5. ```

For use directly in HTML files, you can add the latest script tag from JsDelivr to your HTML file:

  1. ``` html
  2.     <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
  3. ```
See all available versions here

Quick Examples


Example Usage in the Browser

  1. ``` html
  2. <!DOCTYPE html>
  3. <html lang="en">
  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/danfojs@1.1.2/lib/bundle.js"></script>
  8. <title>Document</title>
  9.   </head>
  10. <body>
  11.     <div id="div1"></div>
  12.     <div id="div2"></div>
  13.     <div id="div3"></div>
  14. <script>
  15.       dfd.readCSV("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
  16.           .then(df => {
  17.               df['AAPL.Open'].plot("div1").box() //makes a box plot
  18.               df.plot("div2").table() //display csv as table
  19.               new_df = df.setIndex({ column: "Date", drop: true }); //resets the index to Date column
  20.               new_df.head().print() //
  21.               new_df.plot("div3").line({
  22.                   config: {
  23.                       columns: ["AAPL.Open", "AAPL.High"]
  24.                   }
  25.               })  //makes a timeseries plot
  26.           }).catch(err => {
  27.               console.log(err);
  28.           })
  29.     </script>
  30.   </body>
  31. </html>
  32. ```

Output in Browser:

undefined

Example usage in Nodejs


  1. ``` js
  2. const dfd = require("danfojs-node");

  3. const file_url =
  4.   "https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv";
  5. dfd
  6.   .readCSV(file_url)
  7.   .then((df) => {
  8.     //prints the first five columns
  9.     df.head().print();

  10.     // Calculate descriptive statistics for all numerical columns
  11.     df.describe().print();

  12.     //prints the shape of the data
  13.     console.log(df.shape);

  14.     //prints all column names
  15.     console.log(df.columns);

  16.     // //prints the inferred dtypes of each column
  17.     df.ctypes.print();

  18.     //selecting a column by subsetting
  19.     df["Name"].print();

  20.     //drop columns by names
  21.     let cols_2_remove = ["Age", "Pclass"];
  22.     let df_drop = df.drop({ columns: cols_2_remove, axis: 1 });
  23.     df_drop.print();

  24.     //select columns by dtypes
  25.     let str_cols = df_drop.selectDtypes(["string"]);
  26.     let num_cols = df_drop.selectDtypes(["int32", "float32"]);
  27.     str_cols.print();
  28.     num_cols.print();

  29.     //add new column to Dataframe

  30.     let new_vals = df["Fare"].round(1);
  31.     df_drop.addColumn("fare_round", new_vals, { inplace: true });
  32.     df_drop.print();

  33.     df_drop["fare_round"].round(2).print(5);

  34.     //prints the number of occurence each value in the column
  35.     df_drop["Survived"].valueCounts().print();

  36.     //print the last ten elementa of a DataFrame
  37.     df_drop.tail(10).print();

  38.     //prints the number of missing values in a DataFrame
  39.     df_drop.isNa().sum().print();
  40.   })
  41.   .catch((err) => {
  42.     console.log(err);
  43.   });

  44. ```
Output in Node Console:

undefined

Notebook support

VsCode nodejs notebook extension now supports Danfo.js. See guide here
ObservableHQ Notebooks. See example notebook here


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


Danfo.js - Open Source JavaScript library for manipulating data. | Product Hunt Embed