Class DataFrame

Hierarchy

  • StatisticalOps
    • DataFrame

Constructors

  • Creates a new DataFrame instance.

    Example

    const df = new DataFrame({
    columns: ['Name', 'Age', 'Gender'],
    data: [
    ['Alice', 30, 'Female'],
    ['Bob', 25, 'Male'],
    ['Charlie', 40, 'Male'],
    ],
    });

    Parameters

    Returns DataFrame

Properties

_columns: string[]
_data: any[][]

Accessors

  • get columns(): string[]
  • The columns in the data frame.

    Example

    //Gives an array of column names.
    const columns = df.columns; //['name', 'age', 'gender']

    Returns string[]

    An array of column names.

  • get data(): any[][]
  • The columns in the data frame.

    Example

    //Gives a 2D array .
    const data = df.data; //[['Alice', 30, 'Female'], ['Bob', 25, 'Male'], ['Charlie', 40, 'Male']];

    Returns any[][]

    A two dimensional array of data representing rows of the data frame.

  • get rows(): number
  • The number of rows in the data frame.

    Example

    //Gives the number of rows in the data frame.
    const rows = df.rows; //3

    Returns number

    The number of rows in the data frame.

  • get shape(): [number, number]
  • The shape of the data frame as a tuple of [number of rows, number of columns].

    Example

    const shape = df.shape; // [3, 2]
    

    Returns [number, number]

    A tuple of [number of rows, number of columns].

Methods

  • Parameters

    • firstColumn: string
    • secondColumn: string

    Returns number

  • Returns {
        [column: string]: string;
    }

    • [column: string]: string
  • Parameters

    • columns: string[]

    Returns {
        [key: string]: number;
    }

    • [key: string]: number
  • Parameters

    • columns: string[]

    Returns {
        [key: string]: number;
    }

    • [key: string]: number
  • Parameters

    • columns: string[]

    Returns {
        [key: string]: number;
    }

    • [key: string]: number
  • Parameters

    • columns: string[]

    Returns {
        [key: string]: number;
    }

    • [key: string]: number
  • Adds two columns together and returns the result as a new DataFrame object.

    Example

    //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

    Parameters

    • columnName: string

      The name of the new column to create.

    • column1: string

      The name of the first column to add.

    • column2: string

      The name of the second column to add.

    Returns DataFrame

    A new DataFrame object with the new column added.

  • Example

    const correlation = df.correlation('age', 'height'); //0.5
    

    Parameters

    • firstColumn: string

      The first column to calculate the correlation of.

    • secondColumn: string

      The second column to calculate the correlation of.

    Returns number

    The correlation between the two columns.

  • Returns a detailed string representation of the data frame. Prints all rows.

    Example

    //Print complete rows/data of the data frame.
    console.log(df.details());

    Returns string

    A detailed string representation of the data frame.

  • Drops a column from the data frame.

    Example

    //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']]

    Parameters

    • columnName: string

      The name of the column to drop.

    Returns DataFrame

    A new data frame with the specified column dropped.

  • Filters the DataFrame object by a column value and returns the filtered DataFrame object.

    Example

    //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

    Parameters

    • column: string

      The name of the column to filter by.

    • value: string | number

      The value to filter by.

    Returns DataFrame

    A new DataFrame object with the filtered rows.

  • Returns an object containing the data types of each column in the data frame.

    Example

    //Gives an object containing the data types of each column.
    const columnTypes = df.getColumnTypes(); // {Name: 'string', Age: 'number', Gender: 'string'}

    Returns {
        [column: string]: string;
    }

    An object containing the data types of each column.

    • [column: string]: string
  • Groups the DataFrame object by one or more columns and returns a GroupBy object.

    Example

    //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] ]
    }
    }

    Parameters

    • dataFrame: DataFrame

      The DataFrame object to group.

    • columns: string[]

      An array of column names to group by.

    Returns GroupBy

    A GroupBy object that can be used to aggregate the groups.

  • Returns the first n rows of the data frame.

    Example

    //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);

    Parameters

    • Optional n: number

      The number of rows to return. Defaults to 5.

    Returns DataFrame

    A new data frame containing the first n rows of the data frame.

  • Calculates the mean of the given columns.

    Example

    const mean = df.mean(['age']); // { age: 47.5 }
    

    Parameters

    • columns: string[]

      The columns to calculate the mean of.

    Returns {
        [key: string]: number;
    }

    An object where the keys are the column names and the values are the means.

    • [key: string]: number
  • Calculates the median of the given columns.

    Example

    const median = df.median(['age']); // { age: 47 }
    

    Parameters

    • columns: string[]

      The columns to calculate the median of.

    Returns {
        [key: string]: number;
    }

    An object where the keys are the column names and the values are the medians.

    • [key: string]: number
  • Calculates the mode of the given columns.

    Example

    const mode = df.mode(['age']); // { age: 47 }
    

    Parameters

    • columns: string[]

      The columns to calculate the mode of.

    Returns {
        [key: string]: number;
    }

    An object where the keys are the column names and the values are the modes.

    • [key: string]: number
  • Renames a column in the data frame.

    Example

    //Rename age column to newAge.

    const newDf = df.renameColumn('Age', 'newAge');

    Parameters

    • oldColumnName: string

      The name of the column to rename.

    • newColumnName: string

      The new name for the column.

    Returns DataFrame

    A new data frame with the specified column renamed.

  • Returns a new data frame with only the specified columns.

    Example

    //Gives a new data frame with only the name and age columns.

    const newDf = df.select(['name', 'age']);

    Parameters

    • columns: string[]

      An array of column names to select.

    Returns DataFrame

    A new data frame with only the specified columns.

  • Sorts the DataFrame object by one or more columns and returns the sorted DataFrame object.

    Example

    //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

    Parameters

    • columns: string[]

      An array of column names to sort by.

    • Optional orders: ("asc" | "desc")[] = []

      An array of sort orders for each column. Default is ascending order.

    Returns DataFrame

    A new DataFrame object with the sorted rows.

  • Calculates the standard deviation of the given columns.

    Example

    const standardDeviation = df.standardDeviation(['age']); // { age: 4.5 }
    

    Parameters

    • columns: string[]

      The columns to calculate the standard deviation of.

    Returns {
        [key: string]: number;
    }

    An object where the keys are the column names and the values are the standard deviations.

    • [key: string]: number
  • Returns the last n rows of the data frame.

    Example

    //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);

    Parameters

    • Optional n: number

      The number of rows to return. Defaults to 5.

    Returns DataFrame

    A new data frame containing the last n rows of the data frame.

  • Writes the data frame to a CSV file.

    Example

    //Write the data frame to a CSV file.
    df.toCSV('path/to/file.csv');

    Parameters

    • Optional path: string

      The file path where the CSV file should be saved.

    Returns void

    .

  • Writes the data frame to a JSON file.

    Example

    //Write the data frame to a JSON file.
    df.toJSON('path/to/file.json');

    Parameters

    • Optional path: string

      The file path where the JSON file should be saved.

    Returns void

    .

  • Returns a string representation of the data frame. Prints only first 5 rows and last 5 rows

    Example

    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

    Returns string

    A string representation of the data frame.

  • Creates a new DataFrame from a CSV file.

    Static

    Throws

    If the CSV file is empty.

    Example

    // Create a new DataFrame from a CSV file
    const df = DataFrame.fromCSV('path/to/file.csv');

    Parameters

    • path: string

      The path to the CSV file.

    Returns DataFrame

    A new DataFrame object containing the data from the CSV file.

  • Creates a new DataFrame from a JSON file.

    Throws

    If the JSON file cannot be read or parsed.

    Example

    // Create a new DataFrame from a JSON file
    const df = DataFrame.fromJSON('path/to/file.json');

    Parameters

    • path: string

      The path to the JSON file.

    Returns DataFrame

    A new DataFrame object with the data from the JSON file.

Generated using TypeDoc