Package com.fahmatrix

Class DataFrame

java.lang.Object
com.fahmatrix.DataFrame

public class DataFrame extends Object
DataFrame is the basic object for hadling data

Current Features:
Select/Add column data
Print in System Console
Print data Summary
Import From CSV/TSV, xlsx, Ods, JSON
Export To CSV/TSV, xlsx, Ods, JSON
Reverse (transpose) data
Select Row/Column by Label or Position
Filter By String Opertions (contains, equal, equal ignore case, start with , end with, regex , not empty, custom String Predicate)
  • Constructor Details

    • DataFrame

      public DataFrame()
      Constructor with empty indexes and columns
    • DataFrame

      public DataFrame(List<String> index)
      Constructor with known indexes and empty columns
      Parameters:
      index - Array of indexes
    • DataFrame

      public DataFrame(List<String> index, Map<String,List<Object>> columns)
      Constructor with known indexes and columns
      Parameters:
      index - Array of indexes
      columns - Map of data where key is the column name and the value is Array of cells data
  • Method Details

    • addColumn

      public void addColumn(String name, List<Object> data)
      Add column to the end of data table
      Parameters:
      name - index name
      data - column data (Array of cells data)
    • getColumn

      public Series getColumn(String name)
      return a new Series Object with data of only one column
      Parameters:
      name - column name
      Returns:
      Series data with index
    • getColumnsByLabel

      public DataFrame getColumnsByLabel(String... columnLabels)
      Select Columns by name
      this method assumes you selected all rows
      Parameters:
      columnLabels - Basic String Array (String[]) for column names
      Returns:
      New Dataframe with only the selected data
    • getColumnsByPosition

      public DataFrame getColumnsByPosition(int... columnIndices)
      Select certains columns position
      Parameters:
      columnIndices - Basic Integer Array (int[]) for columns position
      Returns:
      New Dataframe with only the selected datas
    • head

      public DataFrame head()
      Return the first 5 rows as DataFrame Object
      Returns:
      first 5 rows
    • head

      public DataFrame head(int n)
      Return the first n rows as DataFrame Object
      Parameters:
      n - the max number of rows to return
      Returns:
      rows
    • tail

      public DataFrame tail()
      Return the last 5 rows as DataFrame Object
      Returns:
      last 5 rows
    • tail

      public DataFrame tail(int n)
      Return the last n rows as DataFrame Object
      Parameters:
      n - the max number of rows to return
      Returns:
      rows
    • transpose

      public DataFrame transpose()
      Reverse Rows and Columns
      Returns:
      new DataFrame with transposed data
    • getByLabel

      public Object getByLabel(String rowLabel, String colLabel)
      Select Cell by row and column name
      Parameters:
      rowLabel - row name
      colLabel - column name
      Returns:
      Object for cell value (String, Float, Double)
    • getRowsByLabel

      public DataFrame getRowsByLabel(String... rowLabels)
      Select Rows by name
      this method assumes you selected all columns
      Parameters:
      rowLabels - Basic String Array (String[]) for rows name
      Returns:
      New Dataframe with only the selected data
    • getByLabels

      public DataFrame getByLabels(String[] rowLabels, String[] colLabels)
      Select certains rows and columns by name
      these names are not Excel A1,B1 names.
      Parameters:
      rowLabels - Basic String Array (String[]) for row names
      colLabels - Basic String Array (String[]) for column names
      Returns:
      New Dataframe with only the selected data
    • getByPosition

      public Object getByPosition(int rowIdx, int colIdx)
      Select Cell by row and column positions
      Parameters:
      rowIdx - row position
      colIdx - column position
      Returns:
      Object for cell value (String, Float, Double)
    • getRowsByPosition

      public DataFrame getRowsByPosition(int... rowIndices)
      Select certains rows position
      Parameters:
      rowIndices - Basic Integer Array (int[]) for row positions
      Returns:
      New Dataframe with only the selected data
    • getByPositions

      public DataFrame getByPositions(int[] rowIndices, int[] colIndices)
      Select certains rows and columns by position
      Parameters:
      rowIndices - Basic Integer Array (int[]) for row positions
      colIndices - Basic Integer Array (int[]) for column positions
      Returns:
      New Dataframe with only the selected data
    • filterContains

      public DataFrame filterContains(String columnName, String substring)
      Filters rows where the specified column contains the given substring
      Parameters:
      columnName - the name of the column to filter
      substring - the substring to search for
      Returns:
      New Dataframe with only the selected data
    • filterEquals

      public DataFrame filterEquals(String columnName, String value)
      Filters rows where the specified column equals the given value (case-sensitive)
      Parameters:
      columnName - the name of the column to filter
      value - the value to match exactly
      Returns:
      New Dataframe with only the selected data
    • filterEqualsIgnoreCase

      public DataFrame filterEqualsIgnoreCase(String columnName, String value)
      Filters rows where the specified column equals the given value (case-insensitive)
      Parameters:
      columnName - the name of the column to filter
      value - the value to match (ignoring case)
      Returns:
      New Dataframe with only the selected data
    • filterByStringPredicate

      public DataFrame filterByStringPredicate(String columnName, Predicate<String> predicate)
      Filters rows based on a custom string predicate
      Parameters:
      columnName - the name of the column to filter
      predicate - a function that takes a string and returns true if the row should be included
      Returns:
      New Dataframe with only the selected data
    • filterStartsWith

      public DataFrame filterStartsWith(String columnName, String prefix)
      Filter rows where the specified column starts with the given prefix
      Parameters:
      columnName - the name of the column to filter
      prefix - the prefix to match
      Returns:
      New Dataframe with only the selected data
    • filterEndsWith

      public DataFrame filterEndsWith(String columnName, String suffix)
      Filter rows where the specified column ends with the given suffix
      Parameters:
      columnName - the name of the column to filter
      suffix - the suffix to match
      Returns:
      New Dataframe with only the selected data
    • filterRegex

      public DataFrame filterRegex(String columnName, String regex)
      Filter rows where the specified column matches the given regex pattern
      Parameters:
      columnName - the name of the column to filter
      regex - the regular expression pattern to match
      Returns:
      New Dataframe with only the selected data
    • filterNotEmpty

      public DataFrame filterNotEmpty(String columnName)
      Filter rows where the specified column is not null and not empty
      Parameters:
      columnName - the name of the column to filter
      Returns:
      New Dataframe with only the selected data
    • select

      public com.fahmatrix.Helpers.DataSelector select()
      Select using builder pattern
      use like that example
      select().rows(new int[]{1,2,3}).columns(new int[]{1,2,3}).get()
      or
      select().rows(new String[]{"row1","row2","row3"}).columns(new String[]{"column1","column2","column3"}).get()
      Returns:
      Selection Builder Object
    • print

      public void print()
      Pretty Print in System Console
    • describe

      public void describe()
      Pretty Print Data Summary in System Console
    • readCSV

      public DataFrame readCSV(String filePath)
      Read , Parse and save the CSV file
      Make sure the file is found before calling. And it has a proper CSV/TSV format
      All data are saved in the same object no need to create a new one

      Note: it replace any old data

      Parameters:
      filePath - CSV file path
      Returns:
      the same object after saving data (this) if successful
    • writeCSV

      public void writeCSV(String filePath)
      Exports data to a csv file
      It useds default delimiter "," and hasQoutes is set to true
      Parameters:
      filePath - full file path to save ex: ".\\examples\\exampleFiles\\customers-edited.csv"
    • writeCSV

      public void writeCSV(String filePath, char delimiter, boolean hasQuotes)
      Exports data to a csv file

      Parameters:
      filePath - full file path to save ex: ".\\examples\\exampleFiles\\customers-edited.csv"
      delimiter - the csv value delimter ex: ',' or ';' or '|'
      hasQuotes - either wrap the values within qoutes or not
    • readXlsx

      public DataFrame readXlsx(String filePath)
      Read , Parse and save the Microsoft Excel SpreadSheet xlsx file
      Make sure the file is found before calling.
      All data are saved in the same object no need to create a new one

      Note: it replace any old data

      Parameters:
      filePath - xlsx file path
      Returns:
      the same object after saving data (this) if successful
    • writeXlsx

      public void writeXlsx(String filePath)
      Exports data to a Microsoft Excel SpreadSheet xlsx file
      The data is in the default sheet with name "sheet1"
      Parameters:
      filePath - full file path to save ex: ".\\examples\\exampleFiles\\small_data.xlsx"
    • readOds

      public DataFrame readOds(String filePath)
      Read , Parse and save ODS (OpenDocument Spreadsheet) file
      Make sure the file is found before calling.
      All data are saved in the same object no need to create a new one

      Note: it replace any old data

      Parameters:
      filePath - ods file path
      Returns:
      the same object after saving data (this) if successful
    • writeOds

      public void writeOds(String filePath)
      Exports data to an ODS (OpenDocument Spreadsheet) file
      The data is in the default sheet with name "sheet1"
      Parameters:
      filePath - full file path to save ex: ".\\examples\\exampleFiles\\small_data.ods"
    • readJson

      public DataFrame readJson(String filePath)
      Read , Parse and save the JSON file
      Make sure the file is found before calling. And it has a proper JSON format
      All data are saved in the same object no need to create a new one

      Note: it replace any old data
      Note: it supports simple JSON data. (no nesting)

      example of JSON data. [ {"age": 25, "name": "Alice"}, {"age": 30, "name": "Bob"}, {"age": 35, "name": "Charlie"} ]
      Parameters:
      filePath - JSON file path
      Returns:
      the same object after saving data (this) if successful
    • writeJson

      public void writeJson(String filePath)
      Exports data to a JSON txt file

      Parameters:
      filePath - full file path to save ex: ".\\examples\\exampleFiles\\small_data.json"