What is the difference between save a pandas dataf

2019-02-23 14:49发布

I am learning python pandas. I see a tutorial which shows two ways to save a pandas dataframe.

  1. pd.to_csv('sub.csv') and to open pd.read_csv('sub.csv')

  2. pd.to_pickle('sub.pkl') and to open pd.read_pickle('sub.pkl')

The tutorial says to_pickle is to save the dataframe to disk. I am confused about this. Because when I use to_csv, I did see a csv file appears in the folder, which I assume is also save to disk right?

In general, why we want to save a dataframe using to_pickle rather than save it to csv or txt or other format?

1条回答
欢心
2楼-- · 2019-02-23 15:38

Pickle is a serialized way of storing a Pandas dataframe. You are basically writing down the exact representation of your dataframe to disc. This means the types of the columns are the same and the index is the same. If you simply save a file as a csv you are just storing it as a comma separated list. Depending on your data set, some information will be lost when you load it back up.

https://docs.python.org/3/library/pickle.html

查看更多
登录 后发表回答