Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to get a new dataframe based on pandas .loc for java?

 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently posted about using the joinery library in How to filter a dataframe?

I'm now trying to perform a similar idea to the pandas .loc method which is used to "Access a group of rows and columns by label(s) or a boolean array."

Basically the .loc method can be used by giving an array of indices from one dataframe and returning a new dataframe based on the rows corresponding to the indices given.

For ex. in python pandas:

I'm trying to do something similar in java.
In the joinery library there is a method .row() which gives a row back given an index, however it returns as a list and just displays the row but I need a new dataframe with the column headers as well.

So far I have:


To summarize, I'm trying to get a new dataframe, tallTrain, which is based on the indices from the shortAndTall array.

Thanks so much!
 
Bartender
Posts: 4103
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Glenda,

there are at least two ways to get a DataFrame, given a List of row-indices.

First of all: you can get a LIst<Object> (a Row) with the row(int) method.
If you use a List<List<Object>> to collect all these Lists, you can use that List to construct a new DataFrame (see the construcors of a DataFrame.

So the code would be something like:


If the header is missing, you can copy it from the old dataframe and add it to the new DataFrame.

Second: in another topic I described how you could add a column with the values 0, 1, 2, 3.... Now,having done that, you can do a select on that DataFrame, where you can test in the Predicate if the value in that index column is present in the List of indices.

By the way: using the method 'select' will give you a new DataFrame (see the API), so use it like:

 
Glenda Karen
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much!

Piet Souris wrote:


I tried this out and I mostly got it but the rows are inverted so instead of
I got:


Piet Souris wrote:Second: in another topic I described how you could add a column with the values 0, 1, 2, 3.... Now,having done that, you can do a select on that DataFrame, where you can test in the Predicate if the value in that index column is present in the List of indices.

By the way: using the method 'select' will give you a new DataFrame (see the API), so use it like:


That's great thanks so much, just checked the api and seems like I could do a .get() inside select where it equals the indices. I'm gonna try this out.
 
Glenda Karen
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have it working now, needed to use append instead of add:

For adding in the column name, I used add like:

Thanks Piet for all your help!
 
Piet Souris
Bartender
Posts: 4103
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhggg... it sees rhe List as a List of columns, instead as a List of rows (admitted: it says so in the API)!

Well, two possibilities:

if you see a List<List>> as a kind of Matrix, can you 'transpose' that List of Lists? For instance, if we have the List<List>: [ [ 1, 2], [3, 4] ], can you write a method that results in this List: [ [ 1, 3], [2, 4] ]? That can be done in just a few lines of code.

But perhaps easier is to define a new DataFrame, using the headers of your current dataframe (see the API for that).
Then use:

If you look at the API of 'append', you see that it returns a DataFrame. But if you look at the example, it seems to return the same DataFrame, so no worries about creating a lot of DataFrames as you go along.



Edit: I missed you above reply. Hope my reply is still relevant.
Edit 2: yes, you did what I suggste, Well done!
 
Glenda Karen
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your help!
 
Piet Souris
Bartender
Posts: 4103
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic