Slartibartfast
Slartibartfast

Reputation: 1200

How to permanently save data in python after importing from excel

I have a dataframe which look like this:

xl_file
Out[2]: 
                  Day                     Description
Date                                                 
2011-01-26  Wednesday                    Republic Day
2011-03-02  Wednesday                   Mahashivratri
2011-04-12    Tuesday                       Ram Navmi
2011-04-14   Thursday  Dr. Babasaheb Ambedkar Jayanti
2011-04-22     Friday                     Good Friday
              ...                             ...
2020-05-25     Monday          Id-Ul-Fitr (Ramzan ID)
2020-10-02     Friday          Mahatma Gandhi Jayanti
2020-11-16     Monday            Diwali-Balipratipada
2020-11-30     Monday               Gurunanak Jayanti
2020-12-25     Friday                       Christmas

[144 rows x 2 columns]

This was imported from an excel file. I was wondering if there is a way where once it is imported into a dataframe, i can permanently save it somehow. Such that i do not have to keep the excel file and reimported it to dataframe.

Upvotes: 0

Views: 952

Answers (2)

sam
sam

Reputation: 2311

[Update] As mentioned in the comments, If you want to use the data to make executable and do not want a dependency on any other file then you can store it as a dictionary lookup table and load it from that.

data= {<your dictionary created from excel file>}

xl_file = pd.DataFrame.from_dict(data)

Otherwise, you can write the dataframe in the CSV format in the disk

xl_file.to_csv("csv_filename.csv")

If you want to store it as an object, pickle could be the way, however, it can take more memory space.

Upvotes: 1

Aziz Sonawalla
Aziz Sonawalla

Reputation: 2502

Python stores variables (including your imported dataframe) in memory, and memory is volatile (i.e. it only retains data while it's powered). In fact, you lose any data stored in memory as soon as your Python program stops running. So the only way to save data permanently is to save it to disk - which you have already done with your excel file - or save it to a remote location (cloud storage, external DB, etc.)

You can however import the excel file and export it again (to disk) in a way that is easier and faster to parse - this might include either saving it to a different format, for example as a csv (see instructions here), or cleaning-up the dataframe and getting rid of any data you don't need to read.

Upvotes: 1

Related Questions