[C++-sig] Boost Python. access pandas columns in the same order as in python

Vladimir Sakharuk vsakharuk at tower-research.com
Tue Oct 4 09:05:16 EDT 2016


Hello All,


Trying to figure out how to c++ access pandas dataframe columns in the same order as they exist in the python.

in python:

    import pandas


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


    df.columns


    # outputs Index([u'CCC', u'BBB', u'AAA'], dtype='object')

in C++ when passing df to foo.

    foo(const boost::python::object in_obj)


    {


      boost::python::stl_input_iterator<boost::python::object> it (in_obj), 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<<std::endl;   index++;     }     }     //outputs regardless of original order of column names.     //index=0, colname=AAA     //index=1, colname=BBB     //index=2, colname=CCC

As you can see pythons' order 'CCC','BBB','AAA' is not same as c++ 'AAA', 'BBB', 'CCC'. Looks like those order depend on object hasing.


How could I get those values in the original order of pandas dataframe from C++?


Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20161004/f535992f/attachment.html>


More information about the Cplusplus-sig mailing list