ninia / jep

Embed Python in Java
Other
1.3k stars 147 forks source link

Is there an easy way to create a Pandas DataFrame using jep? #120

Closed tomtobin closed 6 years ago

tomtobin commented 6 years ago

I have lists of POJOs in Java and can marshal into a jep.PyJList of jep.PyJMaps using the following:

List<Map<String, Object>> dataMaps = new ArrayList<>();
for (Map record : data.asMaps()) {
    dataMaps.add(record);
}
jep.set("MyData", dataMaps);

I now need this as a DataFrame - tried:

pd.DataFrame(MyData)
pd.DataFrame(list(MyData))
pd.DataFrame.from_records(MyData)

... no luck - any suggestions

ndjensen commented 6 years ago

I'm not familiar with Pandas but presuming it takes a Python dictionary you should be able to convert a PyJMap to a normal Python dictionary. PyJMap implements the Python mapping protocol so it appears as a dictionary to most commands but it's still a Java object that is wrapped inside a Python object. In Java terms, PyJMap can be considered an interface that matches dictionary, but it doesn't support all the operations/functions a dictionary does. So you could try looping over the map and turning it into a dictionary such as

d = {}
for key in MyData:
    d[key] = MyData[key]

Do you have a specific error message? Someone on the mailing list may be able to help too.

bsteffensmeier commented 6 years ago

This issue is inactive and I don't see any bugs or problems originating from jep so I am closing it.