0

I'm new to writing java code as such. I have experience writing code in scripting type languages. I'm trying to rewrite a piece of code I had in python in java.

Python code below -

import pandas as pd
myFile = 'dataFile'
df = pd.DataFrame(pd.read_csv(myFile,skiprows=0))
inData = df.as_matrix()

I'm looking for a method in java that is equivalent to as_matrix in python. This function converts the data frame into a matrix.

I did look up for sometime now but can't find a method as such that does the conversion like in python. Is there a 3rd party library or something on those lines I could use? Any direction would help me a lot please. Thank you heaps.

usert4jju7
  • 1,079
  • 2
  • 18
  • 37
  • what have you tried so far???? [Files.readAllLines](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29) may help... also check this question http://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java – Jordi Castilla Mar 04 '16 at 08:39
  • 1
    You could always write the method yourself and it wouldn't take too much time. It is a simple procedure of opening the csv file, reading line by line and adding to a matrix using the String.split() method. I could elaborate in an answer if you want something like that. – Angelos Chalaris Mar 04 '16 at 08:39
  • I think you want to read a CSV file, you can use OpenCSV for this, it can directly convert your input into beans. – Bhavesh Mar 04 '16 at 08:49
  • Try using the approach here: http://stackoverflow.com/questions/16784014/importing-csv-file-into-2d-string-array – Nikhil Patil Mar 04 '16 at 08:57

1 Answers1

2

What you want to do is really simple and requires minimal code on your part, therefore I suggest you code it yourself. Here is an example implementation:

List<String[]> rowList = new ArrayList<String[]>();
try (BufferedReader br = new BufferedReader(new FileReader("pathtocsvfile.csv"))) {
    String line;
    while ((line = br.readLine()) != null) {
        String[] lineItems = line.split(",");
        rowList.add(lineItems);
    }
    br.close();
}
catch(Exception e){
    // Handle any I/O problems
}
String[][] matrix = new String[rowList.size()][];
for (int i = 0; i < rowList.size(); i++) {
    String[] row = rowList.get(i);
    matrix[i] = row;
}

What this does is really simple: It opens a buffered reader that will read the csv file line by line and paste the contents to an array of Strings after splitting them based on comma (which is your delimiter). Then it will add them to a list of arrays. I know this might not be perfect, so afterwards I take the contents of that list of arrays and turn it into a neat 2D matrix. Hope this helps.

Hint: there are a lot of improvements that could be made to this little piece of code (i.e. take care of trailing and leading spaces, add user-defined delimiters etc.), but this should be a good starting point.

Angelos Chalaris
  • 5,882
  • 7
  • 44
  • 65
  • Hello - I'll try this. The csv file has 16 columns this time around & the rows can be how many ever. Also, the columns in the input file may vary from time to time. Is there a generic way to do this please instead of converting it to a 2D array? – usert4jju7 Mar 05 '16 at 19:13
  • @usert4jju7 What do you mean by generic way? This is the most generic it can be. The matrix is dynamically created based on the size of the data and all the data is saved as strings in it (which you can later manipulate and parse into integers, doubles etc.). – Angelos Chalaris Mar 05 '16 at 19:51