How to serialize/deserialize Pandas DataFrame to and from ProtoBuf/Gzip in a RESTful Flask App? How to serialize/deserialize Pandas DataFrame to and from ProtoBuf/Gzip in a RESTful Flask App? python python

How to serialize/deserialize Pandas DataFrame to and from ProtoBuf/Gzip in a RESTful Flask App?


I ran into the same problem recently. I solved it by iterating through the rows of my DataFrame and calling protobuf_obj.add() in that loop, using info from the DataFrame. You can then GZIP the serialized string output.

i.e. something along the lines of:

for _, row in df.iterrows():    protobuf_obj.add(val1=row[col1], val2=row[col2])proto_str = protobuf_obj.SerializeToString()return gzip.compress(proto_str)

Given that this question hasn't been answered in 9 months, I'm not sure there's a better solution but definitely open to hearing one if there is!