### Quick Project Setup
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Clones the repository, installs dependencies, builds the project, and runs tests for experienced contributors.
```bash
git clone https://github.com/javascriptdata/danfojs.git
cd danfojs
yarn install
yarn build
yarn test
```
--------------------------------
### Install Project Dependencies
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Installs all necessary dependencies for the Danfo.js project using Yarn.
```bash
yarn install
```
--------------------------------
### Install Danfo.js for Node.js
Source: https://github.com/javascriptdata/danfojs/blob/dev/src/danfojs-browser/README.md
Use npm or yarn to install the danfojs-node package for Node.js applications.
```bash
npm install danfojs-node
or
yarn add danfojs-node
```
--------------------------------
### Install Danfo.js for Client-Side Applications
Source: https://github.com/javascriptdata/danfojs/blob/dev/src/danfojs-browser/README.md
Use npm or yarn to install the danfojs package for client-side applications built with frameworks like React, Vue, or Next.js.
```bash
npm install danfojs
or
yarn add danfojs
```
--------------------------------
### JSDoc Example for Series Addition
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Demonstrates JSDoc commenting for a function that adds two series, including parameter and return value documentation, and a usage example.
```javascript
/**
* Add two series of the same length
* @param {Series} series1 - First series to add
* @param {Series} series2 - Second series to add
* @returns {Series} New series containing the sum
*
* @example
* const s1 = new Series([1, 2, 3])
* const s2 = new Series([4, 5, 6])
* const result = add_series(s1, s2)
* // result: Series([5, 7, 9])
*/
function add_series(series1, series2) {
// Implementation
}
```
--------------------------------
### Install Danfo.js for Node.js and Browser
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Install Danfo.js using npm for Node.js (`danfojs-node`) or browser-side applications (`danfojs`). A CDN is also available for direct HTML usage.
```bash
# For Node.js
npm install danfojs-node
# For browser (React, Vue, Next.js, etc.)
npm install danfojs
```
```html
```
--------------------------------
### JSDoc Example for DataFrame Join
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Illustrates JSDoc for a function with an options object, detailing parameters for joining DataFrames.
```javascript
/**
* Join two or more dataframes
* @param {Object} options - Join options
* @param {DataFrame[]} options.df_list - Array of DataFrames to join
* @param {number} options.axis - Join axis (0: index, 1: columns)
* @param {string} options.by_column - Column to join on
* @returns {DataFrame} Joined DataFrame
*/
function join_df(options) {
// Implementation
}
```
--------------------------------
### Generate Date Sequences with Danfo.js `dateRange`
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Create arrays of date strings for time-based indexing. Supports daily, monthly, hourly frequencies and can be used directly as a DataFrame index. Specify start date, period/end date, and frequency.
```javascript
const { dateRange, DataFrame } = require("danfojs-node");
// Daily range: 5 days starting from date
const days = dateRange({ start: "2023-01-01", period: 5, freq: "D" });
console.log(days);
// ["1/1/2023","1/2/2023","1/3/2023","1/4/2023","1/5/2023"]
// Monthly range between two dates
const months = dateRange({ start: "2023-01-01", end: "2023-06-01", freq: "M" });
console.log(months); // Jan through Jun 2023
// Every 2 days
const biDaily = dateRange({ start: "2023-01-01", period: 4, freq: "2D" });
// Hourly range
const hours = dateRange({ start: "2023-01-01", period: 24, freq: "H" });
// Use as a DataFrame index
const df = new DataFrame(
{ value: [10, 20, 30, 40, 50] },
{ index: dateRange({ start: "2023-01-01", period: 5, freq: "D" }) }
);
df.print();
// Rows indexed by date strings
```
--------------------------------
### DataFrame Constructor
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Demonstrates how to create a DataFrame using various input formats like objects of arrays, 2D arrays, and with custom configurations.
```APIDOC
## DataFrame Constructor
### Description
The `DataFrame` constructor accepts 2D arrays, objects of arrays, arrays of objects, or TensorFlow tensors. Optional `index`, `columns`, `dtypes`, and `config` parameters customize the structure.
### Method
`new DataFrame(data, [config])`
### Parameters
- **data**: (Array|Object|Tensor) - The input data for the DataFrame.
- **config**: (Object) - Optional configuration object.
- **columns**: (Array) - Array of column names.
- **index**: (Array) - Array of index labels.
- **dtypes**: (Object) - Object specifying data types for columns.
- **config**: (Object) - Configuration options.
- **lowMemoryMode**: (boolean) - Enables low memory usage mode.
### Request Example
```javascript
const dfd = require("danfojs-node");
const { DataFrame, Series } = dfd;
// From an object of arrays
const df = new DataFrame({
name: ["Alice", "Bob", "Charlie", "Diana"],
age: [25, 30, 35, 28],
salary: [50000.0, 62000.5, 78000.0, 55000.0],
active: [true, false, true, true]
});
df.print();
// From a 2D array with explicit column names and custom index
const df2 = new DataFrame(
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
{ columns: ["A", "B", "C"], index: ["r1", "r2", "r3"] }
);
df2.print();
// Low memory mode for large datasets
const dfLarge = new DataFrame(data, { config: { lowMemoryMode: true } });
```
### Response
Returns a new DataFrame instance.
```
--------------------------------
### Inspect DataFrame with head, tail, and sample
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Quickly view the beginning, end, or a random subset of a DataFrame. `head` and `tail` take an optional number of rows, while `sample` requires the number of rows and an optional seed for reproducibility.
```javascript
const df = new DataFrame({
x: [10, 20, 30, 40, 50, 60, 70, 80],
y: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8]
});
df.head(3).print(); // First 3 rows
df.tail(3).print(); // Last 3 rows
df.head().print(); // Default: first 5 rows
// Random sample (async) — pass seed for reproducibility
const s = await df.sample(4, { seed: 42 });
s.print(); // 4 random rows
```
--------------------------------
### Series Creation and Basic Operations
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Creates a one-dimensional labeled array (Series) and demonstrates basic attribute access, slicing, arithmetic operations, and statistical methods.
```APIDOC
## Series — Creating and Using a Series
A one-dimensional labeled array, equivalent to a single DataFrame column. Supports all arithmetic, statistical, and transformation operations.
### Creating a Series
`new Series(values: Array, options?: { index: Array })`
### Attributes
- **values**: Returns the array of Series values.
- **index**: Returns the index of the Series.
- **dtype**: Returns the data type of the Series.
- **size**: Returns the number of elements in the Series.
### Slicing
- **head(n)**: Returns the first `n` elements.
- **tail(n)**: Returns the last `n` elements.
### Arithmetic Operations
- **add(other, options?: { inplace: boolean })**: Adds `other` to the Series. `other` can be a scalar or another Series.
- **mul(other, options?: { inplace: boolean })**: Multiplies the Series by `other`. `other` can be a scalar or another Series.
- **inplace**: If `true`, modifies the Series in place.
### Statistics
- **mean()**: Calculates the mean of the Series.
- **std()**: Calculates the standard deviation.
- **min()**: Finds the minimum value.
- **max()**: Finds the maximum value.
- **sum()**: Calculates the sum of all elements.
### Value Counts and Uniques
- **valueCounts()**: Returns a Series with counts of unique values.
- **unique()**: Returns an array of unique values.
- **nUnique()**: Returns the number of unique values.
### Example Usage
```javascript
const { Series } = require("danfojs-node");
const sf = new Series([10, 20, 30, 40, 50], { index: ["a","b","c","d","e"] });
console.log(sf.values); // [10, 20, 30, 40, 50]
console.log(sf.index); // ["a","b","c","d","e"]
console.log(sf.dtype); // "int32"
console.log(sf.size); // 5
sf.head(2).print(); // [10, 20]
sf.tail(2).print(); // [40, 50]
// Arithmetic
sf.add(5).print(); // [15, 25, 35, 45, 55]
sf.mul(2).print(); // [20, 40, 60, 80, 100]
const sf2 = new Series([1, 2, 3, 4, 5]);
sf.add(sf2).print(); // [11, 22, 33, 44, 55]
// Inplace operation
sf.add(100, { inplace: true });
console.log(sf.values); // [110, 120, 130, 140, 150]
// Statistics
console.log(sf.mean()); // 130
console.log(sf.min()); // 110
console.log(sf.max()); // 150
console.log(sf.sum()); // 650
// Value counts
const cats = new Series(["cat","dog","cat","bird","dog","cat"]);
cats.valueCounts().print();
// cat=3, dog=2, bird=1
cats.unique().print(); // ["cat","dog","bird"]
cats.nUnique(); // 3
```
```
--------------------------------
### DataFrame Column Selection and Assignment
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Explains how to select existing columns, replace them with new data, and add new columns to a DataFrame.
```APIDOC
## DataFrame Column Selection and Assignment
### Description
Columns are accessed by name using bracket notation. Assignment replaces an existing column or adds a new one, with dtype auto-inferred. The `addColumn` method allows for inserting new columns at specific positions.
### Method
- **Column Access**: `df[columnName]`
- **Column Assignment**: `df[columnName] = newData`
- **Add Column**: `df.addColumn(columnName, data, [config])`
### Parameters
- **columnName**: (string) - The name of the column to access or modify.
- **newData**: (Array|Series) - The new data to assign to the column.
- **data**: (Array|Series) - The data for the new column.
- **config**: (Object) - Optional configuration for `addColumn`.
- **atIndex**: (number) - The zero-based index at which to insert the new column.
- **inplace**: (boolean) - If true, modifies the DataFrame in place.
### Request Example
```javascript
const df = new DataFrame({
alpha: ["A", "B", "C", "D"],
val_count: [1, 2, 3, 4],
val_sum: [20.3, 30.456, 40.9, 90.1]
});
// Read a column (returns Series)
console.log(df["alpha"].values); // ["A", "B", "C", "D"]
// Replace a column with new values
df["alpha"] = ["E", "F", "G", "H"];
// Add a new column
const dfNew = df.addColumn("bonus", [500, 600, 700, 800]) as DataFrame;
// Insert at a specific position
df.addColumn("id", [101, 102, 103, 104], { atIndex: 0, inplace: true });
```
### Response
- **Column Access**: Returns a Series.
- **Column Assignment**: Modifies the DataFrame.
- **Add Column**: Returns a new DataFrame (or modifies in place if `inplace` is true).
```
--------------------------------
### Build Specific Danfo.js Package
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Builds a specific package, such as `danfojs-browser`, after navigating to its directory.
```bash
cd src/danfojs-browser
yarn build
```
--------------------------------
### DataFrame.head, DataFrame.tail, DataFrame.sample
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Quickly inspect the top, bottom, or a random subset of a DataFrame.
```APIDOC
## DataFrame.head, DataFrame.tail, DataFrame.sample
### Description
Quickly inspect the top, bottom, or a random subset of a DataFrame.
### Methods
- `head(n)`: Returns the first `n` rows (default is 5).
- `tail(n)`: Returns the last `n` rows (default is 5).
- `sample(n, [options])`: Returns `n` random rows. `options` can include a `seed` for reproducibility.
### Request Example
```javascript
df.head(3).print(); // First 3 rows
df.tail(3).print(); // Last 3 rows
df.head().print(); // Default: first 5 rows
// Random sample (async) — pass seed for reproducibility
const s = await df.sample(4, { seed: 42 });
s.print(); // 4 random rows
```
```
--------------------------------
### Build Danfo.js Packages
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Builds all packages within the Danfo.js project. Use `yarn dev` for watch mode during development.
```bash
yarn build
```
--------------------------------
### Run All Danfo.js Tests
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Executes all test suites for the Danfo.js project.
```bash
yarn test
```
--------------------------------
### Clone Danfo.js Repository
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Clones your forked Danfo.js repository and navigates into the project directory.
```bash
git clone https://github.com/YOUR_USERNAME/danfojs.git
cd danfojs
```
--------------------------------
### Stage and Commit Changes
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Stages all changes and commits them with a message following Conventional Commits standards.
```bash
git add .
git commit -m "feat: add new feature"
```
--------------------------------
### Run Danfo.js Tests by Pattern
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Executes tests that match a given pattern, such as tests related to DataFrame additions.
```bash
yarn test -g "DataFrame.add"
```
--------------------------------
### Read and Write Excel Files with Danfo.js
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Demonstrates reading data from Excel files (single sheet, specific sheet by name or index, or from a URL) and writing DataFrames to Excel files. Requires the SheetJS (xlsx) library.
```javascript
const dfd = require("danfojs-node");
// Read first sheet (default)
const df = await dfd.readExcel("./data/report.xlsx");
df.head().print();
// Read a specific sheet by name
const df2 = await dfd.readExcel("./data/report.xlsx", {
sheet: "Q1 Sales"
});
// Read a specific sheet by index
const df3 = await dfd.readExcel("./data/report.xlsx", { sheet: 1 });
// Write DataFrame to Excel
dfd.toExcel(df, {
filePath: "./output/report.xlsx",
sheetName: "Results"
});
// Read from URL
const dfRemote = await dfd.readExcel("https://example.com/data.xlsx");
dfRemote.describe().print();
```
--------------------------------
### Run Specific Danfo.js Test File
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Executes tests from a specific file, useful for targeted testing.
```bash
yarn test tests/core/frame.test.js
```
--------------------------------
### Browser Data Loading and Plotting with Danfo.js
Source: https://github.com/javascriptdata/danfojs/blob/dev/README.md
Demonstrates loading CSV data from a URL and performing basic plotting operations like box plots, tables, and time-series line plots in a browser environment. Requires the Danfo.js library to be included via CDN.
```html
Document
```
--------------------------------
### DataFrame `iloc` (Integer-Location Indexing)
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Details on how to select rows and columns using their integer positions with the `iloc` accessor.
```APIDOC
## DataFrame `iloc` (Integer-Location Indexing)
### Description
`iloc` selects rows and columns by zero-based integer position. It supports selecting individual integers, arrays of integers, and slice strings for both rows and columns.
### Method
`df.iloc({ rows: [row_selector], columns: [column_selector] })`
### Parameters
- **rows**: (number|Array|string|Array) - Selector for rows. Can be a single integer, an array of integers, a slice string (e.g., '1:3'), or a boolean array.
- **columns**: (number|Array|string|Array) - Selector for columns. Similar format to `rows`.
### Request Example
```javascript
const df = new DataFrame(
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
{ columns: ["A","B","C"] }
);
// Select rows 0 and 2
df.iloc({ rows: [0, 2] }).print();
// Slice rows 1 through 3 (exclusive end)
df.iloc({ rows: ["1:3"] }).print();
// Select rows 0–1, columns 0 and 2
df.iloc({ rows: ["0:2"], columns: [0, 2] }).print();
// Boolean row selection
df.iloc({ rows: [true, false, true, false] }).print();
// Single row returns DataFrame
console.log(df.iloc({ rows: [0] }).values); // [[1, 2, 3]]
```
### Response
Returns a new DataFrame containing the selected subset of rows and columns.
```
--------------------------------
### Browser: Load CSV and Plot Data with Danfo.js
Source: https://github.com/javascriptdata/danfojs/blob/dev/src/danfojs-browser/README.md
Demonstrates loading a CSV file from a URL in the browser and performing basic plotting operations like box plots, tables, and time-series line plots.
```html
Document
```
--------------------------------
### OneHotEncoder for Categorical Data
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Transforms categorical labels into a binary matrix. Use `fit` to learn categories and `transform` to encode. `fitTransform` combines both steps. `getDummies` is an alias for Series.
```javascript
const { OneHotEncoder } = require("danfojs-node");
const encoder = new OneHotEncoder();
const data = ["cat", "dog", "bird", "cat"];
encoder.fit(data);
const encoded = encoder.transform(data);
console.log(encoded);
// [[1,0,0],[0,1,0],[0,0,1],[1,0,0]]
// columns correspond to: [bird, cat, dog] (sorted order)
```
```javascript
// One-step fit+transform
const result = encoder.fitTransform(["red","green","red","blue"]);
console.log(result);
// [[0,0,1],[0,1,0],[0,0,1],[1,0,0]]
```
```javascript
// getDummies on Series (alias for one-hot encoding)
const { Series } = require("danfojs-node");
const sf = new Series(["male","female","male","male","female"]);
sf.getDummies({ prefix: "gender", prefixSeparator: "_" }).print();
// columns: gender_female, gender_male
```
--------------------------------
### Full Employee Data Processing Pipeline with Danfo.js
Source: https://context7.com/javascriptdata/danfojs/llms.txt
This script demonstrates a complete data processing pipeline using Danfo.js in Node.js. It covers loading CSV data, inspecting and cleaning missing values, feature engineering, encoding categorical variables, scaling numerical features, performing group-by aggregations, filtering data based on conditions, string manipulation, and exporting the processed data to CSV, Excel, and JSON formats. Ensure 'employees.csv' is in the same directory.
```javascript
const dfd = require("danfojs-node");
const {
DataFrame, Series,
LabelEncoder, StandardScaler,
concat, merge
} = dfd;
async function processEmployeeData() {
// 1. Load data
let df = await dfd.readCSV("./employees.csv");
console.log("Shape:", df.shape);
df.dtypes.print();
// 2. Inspect and clean
df.isNa().sum().print(); // count missing values
df.dropNa({ axis: 0, inplace: true }); // drop rows with any NaN
df.drop({ columns: ["id", "notes"], inplace: true }); // remove unused cols
// 3. Feature engineering
df.addColumn("salary_k", df["salary"].div(1000), { inplace: true });
df["years_exp"] = df["years_exp"].round(1);
// 4. Encode categoricals
const deptEncoder = new LabelEncoder();
df["dept_encoded"] = deptEncoder.fitTransform(df["dept"]);
const roleEncoder = new LabelEncoder();
df["role_encoded"] = roleEncoder.fitTransform(df["role"]);
// 5. Scale numeric features
const scaler = new StandardScaler();
const numericCols = df.loc({ columns: ["salary_k","years_exp","age"] });
const scaledCols = scaler.fitTransform(numericCols) as DataFrame;
scaledCols.setIndex({ index: df.index, inplace: true });
// 6. GroupBy analysis
console.log("\n--- Mean salary by department ---");
df.groupby(["dept"]).col(["salary_k"]).mean().print();
console.log("\n--- Aggregated stats ---");
df.groupby(["dept"]).agg({
salary_k: ["mean","max","min"],
years_exp: "mean"
}).print();
// 7. Filter and query
const seniors = df.query(df["years_exp"].gt(5));
console.log("Senior employees:", seniors.shape[0]);
const highEarners = df.query(
df["salary_k"].gt(80).and(df["dept"].eq("Engineering"))
);
highEarners.print();
// 8. String operations
df["name_upper"] = df["name"].str.toUpperCase();
df["first_name"] = df["name"].str.split(" ").map((v) => v[0]);
// 9. Export results
dfd.toCSV(df, { filePath: "./processed_employees.csv" });
dfd.toExcel(seniors, {
filePath: "./senior_employees.xlsx",
sheetName: "Seniors"
});
dfd.toJSON(highEarners, {
filePath: "./high_earners.json",
format: "row"
});
return df;
}
processEmployeeData().catch(console.error);
```
--------------------------------
### Include Danfo.js in HTML Files
Source: https://github.com/javascriptdata/danfojs/blob/dev/src/danfojs-browser/README.md
Add the latest script tag from JsDelivr to your HTML file to use Danfo.js directly in the browser.
```html
```
--------------------------------
### Include Danfo.js in HTML Files
Source: https://github.com/javascriptdata/danfojs/blob/dev/README.md
Add the latest script tag from JsDelivr to your HTML file to use Danfo.js directly in the browser.
```html
```
--------------------------------
### Run Danfo.js Tests in Watch Mode
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Runs tests and automatically re-runs them when file changes are detected.
```bash
yarn test --watch
```
--------------------------------
### Node.js: Read CSV and Perform Data Operations
Source: https://github.com/javascriptdata/danfojs/blob/dev/src/danfojs-browser/README.md
Shows how to read a CSV file in Node.js using Danfo.js, then perform various data manipulation tasks including printing heads, calculating statistics, selecting columns, dropping columns, adding new columns, and checking for missing values.
```javascript
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
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.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);
})
```
--------------------------------
### Group DataFrame by Columns and Aggregate
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Split data into groups based on column values and apply aggregation functions like mean, sum, or max.
```javascript
const df = new DataFrame({
dept: ["eng","eng","mkt","mkt","eng","mkt"],
level: ["jr","sr","jr","sr","sr","jr"],
salary: [60000, 90000, 55000, 80000, 85000, 52000],
bonus: [5000, 12000, 4000, 10000, 11000, 3500]
});
// Group by one column, aggregate all numeric columns
df.groupby(["dept"]).mean().print();
// eng: salary=78333, bonus=9333 / mkt: salary=62333, bonus=5833
// Group by two columns
df.groupby(["dept","level"]).sum().print();
// Aggregate specific columns only
df.groupby(["dept"]).col(["salary"]).mean().print();
// Multiple aggregation operations
df.groupby(["dept"]).agg({
salary: ["sum", "mean"],
bonus: "max"
}).print();
// Get a specific group as a DataFrame
const engGroup = df.groupby(["dept"]).getGroup(["eng"]);
engGroup.print();
// Custom apply function per group
df.groupby(["dept"]).apply((group) => {
return group.head(1); // first row of each group
}).print();
```
--------------------------------
### Create a New Git Branch
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Creates a new branch for a feature or bug fix following Gitflow conventions.
```bash
git checkout -b feature/your-feature-name
```
--------------------------------
### Series Sorting, Ranking, and Deduplication
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Provides methods for sorting Series values, finding the indices of sorted values, identifying extreme values, and removing duplicate entries.
```APIDOC
## Series — Sorting, Ranking, and Deduplication
Provides methods for sorting Series values, finding the indices of sorted values, identifying extreme values, and removing duplicate entries.
### `sortValues(options?: { ascending: boolean })`
Sorts the values in the Series.
- **ascending**: If `true` (default), sorts in ascending order. If `false`, sorts in descending order.
### `argSort(options?: { ascending: boolean })`
Returns the indices that would sort the Series.
- **ascending**: If `true` (default), returns indices for ascending sort. If `false`, returns indices for descending sort.
### `argMax()`
Returns the index of the maximum value in the Series.
### `argMin()`
Returns the index of the minimum value in the Series.
### `dropDuplicates(options?: { keep: 'first' | 'last' | false })`
Removes duplicate values from the Series.
- **keep**: Specifies which duplicate to keep. `'first'` (default) keeps the first occurrence, `'last'` keeps the last occurrence, `false` drops all duplicates.
### Example Usage
```javascript
const { Series } = require("danfojs-node");
const sf = new Series([30, 10, 50, 20, 40]);
// Sort ascending
sf.sortValues().print(); // [10, 20, 30, 40, 50]
// Sort descending
sf.sortValues({ ascending: false }).print(); // [50, 40, 30, 20, 10]
// argSort: indices that would sort
sf.argSort({ ascending: true }).print(); // [1, 3, 0, 4, 2]
// argMax / argMin — position of extremes
console.log(sf.argMax()); // 2 (value 50 is at index 2)
console.log(sf.argMin()); // 1 (value 10 is at index 1)
// Drop duplicate values
const dup = new Series([1, 2, 2, 3, 3, 3, 4]);
dup.dropDuplicates().print(); // [1, 2, 3, 4]
dup.dropDuplicates({ keep: "last" }).print(); // keeps last occurrence [1, 2, 3, 4]
```
```
--------------------------------
### Push Changes to Fork
Source: https://github.com/javascriptdata/danfojs/blob/dev/CONTRIBUTING.md
Pushes the committed changes to your remote fork on GitHub.
```bash
git push origin feature/your-feature-name
```
--------------------------------
### Transform Series Values with Map, Apply, and Replace
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Transform Series data using functions, lookup tables (dictionaries), or direct value replacement. `apply` is an alias for `map` on Series. Use `inplace: true` to modify the Series directly.
```javascript
const sf = new Series([1, 2, 3, 4, 5]);
// map with a function
sf.map((v) => v * v).print(); // [1, 4, 9, 16, 25]
// map with a dictionary (lookup table)
const grades = new Series(["A","B","C","A","B"]);
grades.map({ A: 4.0, B: 3.0, C: 2.0 }).print();
// [4.0, 3.0, 2.0, 4.0, 3.0]
// apply: same as map for Series
sf.apply((v) => v > 3 ? "high" : "low").print();
// ["low","low","low","high","high"]
// replace: swap specific values
const sf2 = new Series([1, 2, 1, 3, 2]);
sf2.replace(1, 99).print(); // [99, 2, 99, 3, 2]
// Inplace map
sf.map((v) => v * 10, { inplace: true });
console.log(sf.values); // [10, 20, 30, 40, 50]
```
--------------------------------
### Create Interactive Plots with Danfo.js
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Shows how to generate various interactive charts (line, bar, scatter, box, histogram, pie) from DataFrames and Series using Plotly.js. Requires a div element with a specific ID for rendering.
```html
```
--------------------------------
### Series String Accessor (`series.str`)
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Provides a set of string manipulation methods for Series objects, such as case conversion, length calculation, searching, slicing, whitespace removal, splitting, and replacing substrings.
```APIDOC
## Series — String Accessor (`series.str`)
String operations available on string-typed Series via the `.str` accessor.
```javascript
const { Series } = require("danfojs-node");
const sf = new Series(["Hello World", "foo bar", "DANFO JS", " trim me "]);
// Case conversion
sf.str.toLowerCase().print(); // ["hello world","foo bar","danfo js"," trim me "]
sf.str.toUpperCase().print(); // ["HELLO WORLD","FOO BAR","DANFO JS"," TRIM ME "]
sf.str.capitalize().print(); // ["Hello world","Foo bar","Danfo js"," trim me "]
// Length
sf.str.len().print(); // [11, 7, 8, 11]
// Searching
sf.str.includes("o").print(); // [true, true, false, false]
sf.str.startsWith("Hello").print(); // [true, false, false, false]
sf.str.endsWith("JS").print(); // [false, false, true, false]
// Extraction
sf.str.slice(0, 5).print(); // ["Hello","foo b","DANFO"," tri"]
sf.str.charAt(0).print(); // ["H","f","D"," "]
// Whitespace removal
sf.str.trim().print(); // ["Hello World","foo bar","DANFO JS","trim me"]
// Split
const csv = new Series(["a,b,c", "x,y,z"]);
csv.str.split(",").print(); // [["a","b","c"],["x","y","z"]]
// Replace substring
sf.str.replace("World", "Danfo").print(); // ["Hello Danfo",...]
```
```
--------------------------------
### Select DataFrame Rows and Columns by Label using loc
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Use `loc` to select subsets of a DataFrame based on row and column labels. It supports individual labels, ranges, and boolean masks for row selection.
```javascript
const df = new DataFrame(
{ score: [88, 92, 75, 60], grade: ["B","A","C","D"] },
{ index: ["alice","bob","charlie","diana"] }
);
// Select by label
df.loc({ rows: ["alice", "charlie"] }).print();
// score=88,grade=B / score=75,grade=C
// Slice from "alice" to "bob" inclusive
df.loc({ rows: ["alice:bob"] }).print();
// Select specific rows and columns
df.loc({ rows: ["bob", "diana"], columns: ["score"] }).print();
// score=92 / score=60
// Boolean filter
const mask = [true, true, false, false];
df.loc({ rows: mask }).print();
// alice and bob only
```
--------------------------------
### Series map, apply, and replace
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Transforms Series values using lookup dictionaries, custom functions, or direct value replacement. Supports inplace modification.
```APIDOC
## Series — `map`, `apply`, and `replace`
Transform Series values using dictionaries, custom functions, or direct value replacement.
### `map(mapping, options?: { inplace: boolean })`
Applies a mapping to the Series. The mapping can be a function or a dictionary.
- **mapping**: A function that takes a value and returns a transformed value, or a dictionary where keys are values to be replaced and values are their replacements.
- **inplace**: If `true`, modifies the Series in place.
### `apply(func, options?: { inplace: boolean })`
Applies a function to each element of the Series. For Series, `apply` is equivalent to `map` with a function.
- **func**: A function that takes a value and returns a transformed value.
- **inplace**: If `true`, modifies the Series in place.
### `replace(oldValue, newValue, options?: { inplace: boolean })`
Replaces all occurrences of `oldValue` with `newValue`.
- **oldValue**: The value to be replaced.
- **newValue**: The value to replace with.
- **inplace**: If `true`, modifies the Series in place.
### Example Usage
```javascript
const { Series } = require("danfojs-node");
const sf = new Series([1, 2, 3, 4, 5]);
// map with a function
sf.map((v) => v * v).print(); // [1, 4, 9, 16, 25]
// map with a dictionary
const grades = new Series(["A","B","C","A","B"]);
grades.map({ A: 4.0, B: 3.0, C: 2.0 }).print();
// [4.0, 3.0, 2.0, 4.0, 3.0]
// apply
sf.apply((v) => v > 3 ? "high" : "low").print();
// ["low","low","low","high","high"]
// replace
const sf2 = new Series([1, 2, 1, 3, 2]);
sf2.replace(1, 99).print(); // [99, 2, 99, 3, 2]
// Inplace map
sf.map((v) => v * 10, { inplace: true });
console.log(sf.values); // [10, 20, 30, 40, 50]
```
```
--------------------------------
### OneHotEncoder
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Transforms categorical labels into a binary matrix. Each unique category is converted into its own binary column.
```APIDOC
## `OneHotEncoder` — Binary Matrix Encoding
Transform categorical labels into a binary matrix where each category becomes its own column.
```javascript
const { OneHotEncoder } = require("danfojs-node");
const encoder = new OneHotEncoder();
const data = ["cat", "dog", "bird", "cat"];
encoder.fit(data);
const encoded = encoder.transform(data);
console.log(encoded);
// [[1,0,0],[0,1,0],[0,0,1],[1,0,0]]
// columns correspond to: [bird, cat, dog] (sorted order)
// One-step fit+transform
const result = encoder.fitTransform(["red","green","red","blue"]);
console.log(result);
// [[0,0,1],[0,1,0],[0,0,1],[1,0,0]]
// getDummies on Series (alias for one-hot encoding)
const { Series } = require("danfojs-node");
const sf = new Series(["male","female","male","male","female"]);
sf.getDummies({ prefix: "gender", prefixSeparator: "_" }).print();
// columns: gender_female, gender_male
```
```
--------------------------------
### DataFrame Structural Mutations
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Learn how to modify the structure of a DataFrame by dropping or renaming columns and rows, and managing the index.
```APIDOC
## DataFrame — `drop`, `rename`, `setIndex`, `resetIndex`
Structural mutations: removing rows/columns, renaming labels, and managing the index.
```javascript
const df = new DataFrame({
id: [1, 2, 3, 4],
name: ["Alice","Bob","Charlie","Diana"],
age: [25, 30, 35, 28],
salary: [50000, 62000, 78000, 55000]
});
// Drop columns
const df2 = df.drop({ columns: ["id", "age"] }) as DataFrame;
console.log(df2.columns); // ["name", "salary"]
// Drop rows by index label
const df3 = df.drop({ index: [0, 2] }) as DataFrame;
df3.print(); // rows for Bob and Diana
// Rename columns
const df4 = df.rename({ name: "full_name", age: "years" }) as DataFrame;
console.log(df4.columns); // ["id", "full_name", "years", "salary"]
// Set a column as the index
const dfIndexed = df.setIndex({ column: "name", drop: true }) as DataFrame;
dfIndexed.print();
// Index is now ["Alice","Bob","Charlie","Diana"]
// Reset index back to integers
dfIndexed.resetIndex({ inplace: true });
// Transpose (swap rows and columns)
df.transpose().print(); // also: df.T
```
```
--------------------------------
### Encode Categorical Labels with Danfo.js `LabelEncoder`
Source: https://context7.com/javascriptdata/danfojs/llms.txt
Convert string labels into numerical codes and back. Use `fit` to learn the mapping and `transform` to encode, or `fitTransform` for a single step. Works with arrays and Series.
```javascript
const { LabelEncoder, Series, DataFrame } = require("danfojs-node");
const encoder = new LabelEncoder();
const labels = ["cat", "dog", "bird", "cat", "dog", "bird", "cat"];
encoder.fit(labels);
// Encode to integers
const encoded = encoder.transform(labels);
console.log(encoded); // [0, 1, 2, 0, 1, 2, 0] (order depends on first seen)
// Decode back
const decoded = encoder.inverseTransform(encoded);
console.log(decoded); // ["cat","dog","bird","cat","dog","bird","cat"]
// Fit and transform in one step
const result = encoder.fitTransform(["red","green","blue","red"]);
console.log(result); // [0, 1, 2, 0]
// Encoding a Series
const categorySeries = new Series(["apple","orange","apple","banana"]);
const encSeries = encoder.fitTransform(categorySeries);
console.log(encSeries); // [0, 1, 0, 2]
```