lundi 23 janvier 2017

Extract items from a dataframe

I have a boost::python class like this that I have compiled into an .so to be used from python:

class DFClass
{
public:
    DFClass(std::string n) : name(n) {}

    std::string name;

    boost::python::object getDF() const { return dataframeObj; }
    void setDF(boost::python::object in_obj) 
    { 
        dataframeObj = in_obj; 

        boost::python::stl_input_iterator<boost::python::object> it (dataframeObj), end;
        int index=0;
        for(; it != end; it++)     
        {    
            auto extracted = boost::python::extract<char const *> (*it);   
            char const *colname = extracted;     
            std::cout<<"index=" << index << ", colname="<< colname << *it->second << std::endl;   
            index++;     
        }
    }

private:
    boost::python::object dataframeObj;
};

BOOST_PYTHON_MODULE(member1)
{
    class_<DFClass>("DFClass", init<std::string>()) 
        .def_readwrite("name", &DFClass::name)       
        .add_property("dataframe", &DFClass::getDF, &DFClass::setDF)
    ;

}

setDF correctly prints the names of the columns, but how do I get the corresponding row elements from a similar or the same iterator?

import pandas
import member1

df = pandas.DataFrame.from_items([('CCC', [5]), ('BBB', [1]), ('AAA', [4])])
#print df.columns
#print df

dat = member1.DFClass("DataFrame")
dat.dataframe = df

print dat.dataframe

When I run the code it works fine, but I need the actual row values for each column:

[idf@node3 python]$ python testdf.py 
index=0, colname=CCC
index=1, colname=BBB
index=2, colname=AAA
   CCC  BBB  AAA
0    5    1    4

Aucun commentaire:

Enregistrer un commentaire