'How to store multiple numpy.ndarray into a csv file?
I want to store multiple PIL.Image that are converted to np.ndarray into pd.DataFrame and then store it into a csv file in order to send it to my mlflow model.
How can I load the dataframe just like the dataframe that I saved?
The data type will change after saving into .csv format as you can see.
Code:
import numpy as np
import pandas as pd
from PIL import Image
img_0 = np.asarray(Image.open("dataset/image_0.jpg"))
img_1 = np.asarray(Image.open("dataset/image_1.jpg"))
df = pd.DataFrame({
'images': [img_0, img_1],
})
print(df['images'][0])
print(type(df['images'][0]))
df.to_csv('test.csv', index=False)
df = pd.read_csv('test.csv')
print(df['images'][0])
print(type(df['images'][0]))
Output:
<class 'numpy.ndarray'>
<class 'str'>
And the data in df['images'][0] is changed from
[[[ 38 27 23]
[ 36 25 21]
[ 33 22 18]
...
[ 30 24 24]
[ 32 26 26]
[ 33 27 27]]
[[ 39 28 24]
[ 36 25 21]
[ 33 22 18]
...
[ 30 24 24]
[ 30 24 24]
[ 31 25 25]]
[[ 39 28 24]
[ 36 25 21]
[ 34 23 19]
...
[ 29 23 23]
[ 28 22 22]
[ 28 22 22]]
...
to
'[[[ 38 27 23]\n [ 36 25 21]\n [ 33 22 18]\n ...\n [ 30 24 24]\n [ 32 26 26]\n [ 33 27 27]]\n\n [[ 39 28 24]\n [ 36 25 21]\n [ 33 22 18]\n ...\n [ 30 24 24]\n [ 30 24 ...
And the error that I encounter is
Traceback (most recent call last):
...
File "/home/matin/workspace/CI/computational-intelligence-final-project/image_classification_resnet.py", line 158, in <lambda>
df['images'] = df['images'].apply(lambda x: Image.fromarray(np.uint8(x)))
ValueError: invalid literal for int() with base 10: '[[[ 38 27 23]\n [ 36 25 21]\n [ 33 22 18]\n ...\n [ 30 24 24]\n [ 32 26 26]\n [ 33 27 27]]\n\n [[ 39 28 24]\n [ 36 25 21]\n [ 33 22 18]\n ...\n [ 30 24 24]\n [ 30 24
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
