« Pandas
path ( file ) | Path with file name to store the byte string of pickled data |
compression | (Optional )Type of compression, {'infer', 'gzip', 'bz2', 'zip', 'xz', None} |
protocol | (Optional) , Int, we can use HIGHEST_PROTOCOL to get the latest |
We can create a file with DataFrame data by using to_pickle(). We will create one DataFrame by using a dictionary.
import pandas as pd
import pickle
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
my_data = pd.DataFrame(data=my_dict)
my_data.to_pickle("my_data.pkl") # pickled
This is the data written to the current directory. Check for the file my_data.pkl . You can use path to store the file in different location.
my_data.to_pickle("D:\my_data\my_data.pkl")
Compression
We can add compression option. ( You must take care while reading the same file )
my_data.to_pickle("my_data.pkl",compression='zip')
protocol
We can assign the integer or we can get the latest by using HIGHEST_PROTOCOL
my_data.to_pickle("my_data.pkl",protocol=pickle.HIGHEST_PROTOCOL)
While creating the pickle we have to note the compression used. When we de-serialize or un-pickle the same file, we must use the same type of compression.
read_pickle()»
Read how to pickle data stored in a MySQL table.
« Pandas
read_clipboard()
read_html()
read_csv()
read_excel()
to_excel()
« Data input and output from Pandas DataFrame