+4 votes
in Programming Languages by (75.9k points)
I want to save a pandas dataframe to a pickle file and later read the data from the saved file. How can I read/write pickle files using the pandas module?

1 Answer

+1 vote
by (353k points)
selected by
Best answer

You can use the following functions of the pandas module to read/write a pickle file.

to_pickle(): save dataframe in a pickle file.
read_pickle(): load data from a pickle file.

Here is an example using these functions:

import pandas as pd
df = pd.DataFrame({"name": ['AA', 'BB', 'CC', 'DD', 'EE', 'HH', 'II'], "age": [34, 12, 56, 43, 23, 41, 52]})

# save dataframe to a pickle file
pd.to_pickle(df, "dummyTest.pkl")

# load dataframe from the pickle file
df1 = pd.read_pickle("dummyTest.pkl")

The above code will print the following output:

  name  age
0   AA   34
1   BB   12
2   CC   56
3   DD   43
4   EE   23
5   HH   41
6   II   52