The options for creating the DataFrame.
Protected
_columnsProtected
_dataThe columns in the data frame.
//Gives an array of column names.
const columns = df.columns; //['name', 'age', 'gender']
An array of column names.
The columns in the data frame.
//Gives a 2D array .
const data = df.data; //[['Alice', 30, 'Female'], ['Bob', 25, 'Male'], ['Charlie', 40, 'Male']];
A two dimensional array of data representing rows of the data frame.
The number of rows in the data frame.
//Gives the number of rows in the data frame.
const rows = df.rows; //3
The number of rows in the data frame.
The shape of the data frame as a tuple of [number of rows, number of columns].
const shape = df.shape; // [3, 2]
A tuple of [number of rows, number of columns].
Protected
_addProtected
_correlationProtected
_detailsProtected
_dropProtected
_filterProtected
_getProtected
_groupProtected
_headProtected
_meanProtected
_medianProtected
_modeProtected
_renameProtected
_selectProtected
_sortProtected
_standardProtected
_tailProtected
_toCSVProtected
_toJSONProtected
_toAdds two columns together and returns the result as a new DataFrame object.
//Add a new column whose values = sum('col1', 'col2')
const newDf = df.addColumns("col1 + col2", "col1", "col2");
//Output:
col1 col2 col1 + col2
1 1 2 3
2 2 3 5
3 3 4 7
4 4 5 9
5 5 6 11
The name of the new column to create.
The name of the first column to add.
The name of the second column to add.
A new DataFrame object with the new column added.
const correlation = df.correlation('age', 'height'); //0.5
The first column to calculate the correlation of.
The second column to calculate the correlation of.
The correlation between the two columns.
Drops a column from the data frame.
//Drop age column from the data frame.
const df = new DataFrame({
columns: ['Name', 'Age', 'Gender'],
data: [
['Alice', 30, 'Female'],
['Bob', 25, 'Male'],
['Charlie', 40, 'Male'],
],
});
const newDf = df.dropColumn('Age');
console.log(newDf.data); //[['Alice', 'Female'], ['Bob', 'Male'], ['Charlie', 'Male']]
The name of the column to drop.
A new data frame with the specified column dropped.
Filters the DataFrame object by a column value and returns the filtered DataFrame object.
//filter data frame based on age = 21
const newDf = df.filter("age", 21);
//Output:
name age
1 Jack 21
2 Jone 21
3 Paul 21
4 Tim 21
The name of the column to filter by.
The value to filter by.
A new DataFrame object with the filtered rows.
Returns an object containing the data types of each column in the data frame.
//Gives an object containing the data types of each column.
const columnTypes = df.getColumnTypes(); // {Name: 'string', Age: 'number', Gender: 'string'}
An object containing the data types of each column.
Groups the DataFrame object by one or more columns and returns a GroupBy object.
//group the data frame by gender column
const groupedData = df.groupBy(df, ["gender"]);
//Output:
{
Female: DataFrame {
_columns: [ 'Name', 'Age', 'Gender' ],
_data: [ [Array] ]
},
Male: DataFrame {
_columns: [ 'Name', 'Age', 'Gender' ],
_data: [ [Array], [Array] ]
}
}
The DataFrame object to group.
An array of column names to group by.
A GroupBy object that can be used to aggregate the groups.
Returns the first n rows of the data frame.
//Give a new data frame containing the first 5 rows of the data frame.
const newDf = df.head();
or
//Give a new data frame containing the first 3 rows of the data frame.
const newDf = df.head(3);
Optional
n: numberThe number of rows to return. Defaults to 5.
A new data frame containing the first n rows of the data frame.
Calculates the mean of the given columns.
const mean = df.mean(['age']); // { age: 47.5 }
The columns to calculate the mean of.
An object where the keys are the column names and the values are the means.
Calculates the median of the given columns.
const median = df.median(['age']); // { age: 47 }
The columns to calculate the median of.
An object where the keys are the column names and the values are the medians.
Calculates the mode of the given columns.
const mode = df.mode(['age']); // { age: 47 }
The columns to calculate the mode of.
An object where the keys are the column names and the values are the modes.
Renames a column in the data frame.
//Rename age column to newAge.
const newDf = df.renameColumn('Age', 'newAge');
The name of the column to rename.
The new name for the column.
A new data frame with the specified column renamed.
Returns a new data frame with only the specified columns.
//Gives a new data frame with only the name and age columns.
const newDf = df.select(['name', 'age']);
An array of column names to select.
A new data frame with only the specified columns.
Sorts the DataFrame object by one or more columns and returns the sorted DataFrame object.
//Sort data frame by age column in descending order.
const newDf = df.sort(["age"], ["desc"]);
//Output:
name age
1 Jack 26
2 Jone 25
3 Paul 22
4 Tim 21
An array of column names to sort by.
Optional
orders: ("asc" | "desc")[] = []An array of sort orders for each column. Default is ascending order.
A new DataFrame object with the sorted rows.
Calculates the standard deviation of the given columns.
const standardDeviation = df.standardDeviation(['age']); // { age: 4.5 }
The columns to calculate the standard deviation of.
An object where the keys are the column names and the values are the standard deviations.
Returns the last n rows of the data frame.
//Give a new data frame containing the last 5 rows of the data frame.
const newDf = df.tail();
or
//Give a new data frame containing the last 3 rows of the data frame.
const newDf = df.tail(3);
Optional
n: numberThe number of rows to return. Defaults to 5.
A new data frame containing the last n rows of the data frame.
Returns a string representation of the data frame. Prints only first 5 rows and last 5 rows
const df = new DataFrame({
columns: ['Name', 'Age', 'Gender'],
data: [
['Alice', 30, 'Female'],
['Bob', 25, 'Male'],
['Charlie', 40, 'Male'],
],
});
console.log(df.toString());
//Output:
DataFrame
Shape: (3,3)
Name Age Gender
1 Alice 30 Female
2 Bob 25 Male
3 Charlie 40 Male
A string representation of the data frame.
Static
fromCSVCreates a new DataFrame from a CSV file.
If the CSV file is empty.
// Create a new DataFrame from a CSV file
const df = DataFrame.fromCSV('path/to/file.csv');
The path to the CSV file.
A new DataFrame object containing the data from the CSV file.
Static
fromJSONCreates a new DataFrame from a JSON file.
If the JSON file cannot be read or parsed.
// Create a new DataFrame from a JSON file
const df = DataFrame.fromJSON('path/to/file.json');
The path to the JSON file.
A new DataFrame object with the data from the JSON file.
Generated using TypeDoc
Creates a new DataFrame instance.
Example