Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using the joinery library to use a DataFrame in my project. I was able to use it to read a csv file
Java
DataFrame<Object> shortAndTall = DataFrame.readCsv(myDataFileName);

but now I'm trying to figure out how I can filter my dataframe?

What I have tried:

For ex. in the csv I have a column height and the value is either t - tall or s - short, I'm trying to figure out a way to get the dataframe for each of those, something like:
Java
onlyTall = shortAndTall[shortAndTall.height == "t"]  
onlyShort = shortAndTall[shortAndTall.height == "s"]

When I try this I get:
The type of the expression must be an array type but it resolved to DataFrame<Object>

Any ideas how I can filter the dataframe properly?

Thanks so much!
Posted
Updated 16-Mar-21 2:54am

1 solution

Okay, so I looked at their documentation (partly for self too to see how are they using it compared to Python). What you are trying above is a Python type code in Java. That would not work out.

Before going to how, about the error:
Quote:
The type of the expression must be an array type but it resolved to DataFrame<Object>

This is because, shortAndTall is of type DataFrame<object>. Object -> not am array or a list. Thus, when you try to mask and get comparison code, you will get an error. General case, you would need to cast before use. But, not in this case.

Now, based on the documentation there, it seems you need to use Predicate Interface for any filter. If it's difficult for you, do a Cntrl+F for Predicate word: DataFrame| Joinery[^]
An example of filter there is:
Java
DataFrame<Object> df = new DataFrame<>("name", "value");
for (int i = 0; i < 10; i++)
    df.append(Arrays.asList("name" + i, i));

df.select(new Predicate<Object>() {
        @Override
        public Boolean apply(List<Object> values) {
             return Integer.class.cast(values.get(1)).intValue() % 2 == 0;
        
     })
  .col(1);
// Output [0, 2, 4, 6, 8] }  

You have chosen the library for working with dataframe and thus you need to follow their documentation and the code expected to get the results.

It would be better to go through their documentation in detail, understand it and then refer it if you plan to use it.
 
Share this answer
 
Comments
ynjay 7-Oct-20 10:01am    
Thank you! So I looked into it more and I was able to get some things working, now I'm stuck trying to save the new short and tall rows and columns into there own dataframe.
What I have so far is:
DataFrame<object> shortAndTall = DataFrame.readCsv(myDataFile);

shortAndTall.select((Predicate<object>) new Predicate<object>() {
@Override
public Boolean apply(List<object> record) {
String height = (String) record.get(2);
// System.out.println(height);
return Fall_ADL.equals("t");
}});
Sandeep Mewara 7-Oct-20 10:06am    
Good to know it helped. Would suggest to open another query in case you are facing different issue now. I am into middle of something, maybe someone else be able to help.
ynjay 7-Oct-20 13:00pm    
Thank you, I will open a new question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900