Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a column in H2OFrame to a python list?

I've read the PythonBooklet.pdf by H2O.ai and the python API documentation, but still can't find a clean way to do this. I know I can do either of the following:

  • Convert H2OFrame to Spark DataFrame and do a flatMap + collect or collect + list comprehension.
  • Use H2O's get_frame_data, which gives me a string of header and data separated by \n; then convert it a list (a numeric list in my case).

Is there a better way to do this? Thank you.

like image 355
BlueFeet Avatar asked Apr 03 '17 16:04

BlueFeet


2 Answers

You can try something like this: bring an H2OFrame into python as a pandas dataframe by calling .as_data_frame(), then call .tolist() on the column of interest.

A self contained example w/ iris

import h2o
h2o.init()
df = h2o.import_file("iris_wheader.csv")
pd = df.as_data_frame()
pd['sepal_len'].tolist()
like image 93
Nick Karpov Avatar answered Oct 23 '22 20:10

Nick Karpov


You can (1) convert the H2o frame to pandas dataframe and (2) convert pandas dataframe to list as follows:

pd=h2o.as_list(h2oFrame) 
l=pd["column"].tolist()
like image 37
Hajar Homayouni Avatar answered Oct 23 '22 20:10

Hajar Homayouni