DEV Community

mouso
mouso

Posted on

Java CSV data selection the easy way.

Introduction

Our hero today is a new java library called Fahmatrix.

Fahmatrix is a lightweight, modern Java library for working with tabular data, inspired by Python's Pandas and rooted in the idea of making data understanding (fahm) easy on the JVM.

you can find it on Github

Step 1 (installation)

  1. Goto releases page and download the jar file.
  2. I use Eclipse.
  3. Add jar file to ./libs folder.
  4. Right click on project from Eclipse IDE.
  5. choose Java Build Path.
  6. Add Extrenal Java File.
  7. Choose the file ./libs/fahmatrix-vxxx.jar

Step 2 (import data)

  1. create class SimpleCSV.java
  2. add the following code.
  3. replace path//to//data.csv with your actual data file path

    import com.fahmatrix.DataFrame;
    import com.fahmatrix.Series;
    
    public class SimpleCSV {
    
        public static void main(String[] args) {
    
            DataFrame df = new DataFrame();
            df.readCSV("path//to//data.csv");
    
        }
    
    }
    

Step 3 (select data)

    // select rows by position
    DataFrame result = df.getRowsByPosition(new int[]{1,2,3,4,5,10,11,12,13,14,15});
    // select columns by name
    result = result.getColumnsByLabel(new String[]{"name","address","city"});
Enter fullscreen mode Exit fullscreen mode

now you have only the needed data.

Step 4 (view data in console)

just call print()

    result.print();
Enter fullscreen mode Exit fullscreen mode




Final Thoughts

Fahmatrix is a new java library to make data operations easy. i hope it continues development as we want.

Top comments (0)