I have a pandas dataframe with different data type on it.
and i want to make it usable in c++, for performance reason, i want c++ read it in binary format.
for example:
In [4]: df = pd.DataFrame(np.reshape(range(9), (3, 3)))
In [5]: df
Out[5]:
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
In [6]: df['ticker'] = 'helloworld'
In [7]: df['float'] = 1.12
In [8]: df
Out[8]:
0 1 2 ticker float
0 0 1 2 helloworld 1.12
1 3 4 5 helloworld 1.12
2 6 7 8 helloworld 1.12
i tried numpy to_bytes, but seems not work.
with open('a.bin', 'wb') as f:
f.write(df.values.tobytes())
c++ read:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
std::fstream f("a.bin", std::ios::binary | std::ios::in);
char buff[256];
f.read(buff, sizeof(buff));
cout << *(int*)(buff + 2*sizeof(int)) << endl; // should be 2, but get 0
}
how can i dump it out in a binary format, and let c++ read it?