Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save pandas dataframe with numpy arrays column

Let us consider the following pandas dataframe:

df = pd.DataFrame([[1,np.array([6,7])],[4,np.array([8,9])]], columns = {'A','B'})

enter image description here

where the B column is composed by two numpy arrays.

If we save the dataframe and the load it again, the numpy array is converted into a string.

df.to_csv('test.csv', index = False)
df.read_csv('test.csv')

Is there any simple way of solve this problem? Here is the output of the loaded dataframe.

enter image description here

like image 940
nunodsousa Avatar asked Jan 24 '18 17:01

nunodsousa


1 Answers

you can pickle the data instead.

df.to_pickle('test.csv')
df = pd.read_pickle('test.csv')

This will ensure that the format remains the same. However, it is not human readable

If human readability is an issue, I would recommend converting it to a json file

df.to_json('abc.json')
df = pd.read_json('abc.json')
like image 77
Usernamenotfound Avatar answered Nov 16 '22 05:11

Usernamenotfound