1

So if I make a dictionary like

dictionary = {'first': array([1,2,3,4,5]), 'second': array([1,2,3,4,5])}

and then if have to somehow save it to a file, how do I do it? I tried pickling but it take a long time to pack and unpack the data at the start and end of the program as I am going to use a big dataset.

What should I do?

0

3 Answers 3

1

Using numpy.save and numpy.load is much faster

import numpy as np
dictionary = {'first': array([1,2,3,4,5]), 'second': array([1,2,3,4,5])}

# to save
np.save('data.npy', dictionary)

# to load
dictionary = np.load('data.npy')
Sign up to request clarification or add additional context in comments.

1 Comment

when I load it again, its shows as an ndarray and now i cant use it as a dictionary and and use .keys(), .values(), etc. also I cant typecast it back into a dict when I load it again.
0

Create a dictionary then use the dictionary to create a dataframe then call the to_csv method

dictionary = {'first': [1,2,3,4,5], 'second': [1,2,3,4,5]}
df=pd.DataFrame(dictionary)
df.to_csv('myfile.csv')

Comments

0

How about using pandas to_csv function to save it into a csv file:

import pandas as pd
dictionary = {'first': np.array([1,2,3,4,5]), 'second': np.array([1,2,3,4,5])}
pd.DataFrame(dictionary).to_csv('data.csv')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.