2

I am trying to send a dataframe using socket programming.For that I need to know how to encode a dataframe into bytes at the client and then decode it into a dataframe at the server.

1 Answer 1

6

You can use the cPickle or pickle module.

First serialize the dataframe:

>>> df A B 0 1 3 1 2 4 >>> df_bytes = cPickle.dumps(df) 

Send the contents of df_bytes somehow, then deserialize:

>>> df2 = cPickle.loads(df_bytes) >>> df2 A B 0 1 3 1 2 4 

These two modules does introduce potential security issues as they allow executing arbitrary code during deserialization. A better solution would be to write the dataframe into a json string:

df_string = df.to_json() 

And recover it afterwards:

df_again = pandas.read_json(df_string) 

Other formats such as csv are also available, the corresponding functions are DataFrame.to_csv() and pandas.read_csv(). See this and this for a complete list of related functions.

Sign up to request clarification or add additional context in comments.

3 Comments

Could not find the package.Plus I read it is not secure.Any other suggestions?
These two modules are in the standard library. So no additional package is needed. Simply import pickle or import cPickle will do. I have edited the post to add methods converting to and from string formats, which should be more secure.
Thanks Man. I tried with pickle.It is working. Security is not so much of an issue as of now.So going to use pickle for now. Will try the json later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.