Data analysis in JavaScript has just leveled up with **Danfo.js**! This toolkit is fast, flexible, and perfect for working with data structures. Whether you’re building applications or performing data analysis, this guide will take you through the essentials of Danfo.js, helping you navigate its features with ease.
What is Danfo.js?
**Danfo.js** is a JavaScript package inspired by the popular Python library Pandas. It offers a robust set of data structures, making it easier to work with relational or labeled data. If you’re familiar with Pandas, picking up Danfo.js will be a breeze!
Main Features
- Fast and supports TensorFlow.js tensors, allowing easy conversion of data structures to tensors.
- Efficient handling of missing data represented as NaN.
- Dynamic size mutability for columns, enabling easy insertion and deletion.
- Automatic and explicit data alignment to labels.
- Advanced groupby functionality for complex data transformations.
- Seamless conversion of various data types (Arrays, JSONs, Tensors) into DataFrame objects.
- Intuitive data slicing, indexing, and querying.
- Robust I/O tools for loading data from flat files like CSV, JSON and Excel.
- Powerful plotting capabilities for visualizing data interactively.
- Specialized functionalities for handling time series data.
Installation
Installing Danfo.js can be done in three ways:
- For Node.js applications, use npm or yarn:
npm install danfojs-node
yarn add danfojs-node
npm install danfojs
yarn add danfojs
<script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.js"></script>
Example Usage in the Browser
Here’s a simple example showing how to use Danfo.js in an HTML file:
<!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
let 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 time series plot
});
})
.catch(err => console.log(err));
</script>
</body>
</html>
Example Usage in Node.js
For Node.js applications, here’s how to get started:
const dfd = require("danfojs-node");
const file_url = "https://web.stanford.edu/class/archive/cs/cs/cs109/cs109.1166/stuff/titanic.csv";
dfd.readCSV(file_url)
.then((df) => {
df.head().print(); // prints the first five columns
df.describe().print(); // Calculate descriptive stats
console.log(df.shape); // prints the shape of data
console.log(df.columns); // prints column names
df.ctypes.print(); // prints inferred dtypes
// Selecting, dropping, and adding columns
df["Name"].print(); // selecting a column
let cols_2_remove = ["Age", "Pclass"];
let df_drop = df.drop({columns: cols_2_remove, axis: 1});
df_drop.print();
let new_vals = df["Fare"].round(1);
df_drop.addColumn("fare_round", new_vals, {inplace: true});
df_drop.print();
})
.catch((err) => console.log(err));
Troubleshooting
If you run into any issues while installing or using Danfo.js, consider the following troubleshooting steps:
- Ensure that your Node.js installation is up-to-date, as Danfo.js requires a compatible version.
- Check for typos in the code snippets provided in the examples, especially concerning variable names and file paths.
- If facing issues with missing data, review how data is imported and ensure that there are no discrepancies in the CSV files.
- Use console logs to debug any unexpected outputs in your data manipulations.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
Danfo.js brings the power of data analysis to JavaScript, making it easier to work with data in various formats. Whether you’re analyzing complex datasets or simply need quick results, Danfo.js is your go-to toolkit.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.