0

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.

Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • 2
    Does this answer your question? https://stackoverflow.com/questions/17098654/how-to-store-a-dataframe-using-pandas – Prateek Dewan May 25 '20 at 19:46
  • Permanently save it where? It already was saved in a file format – roganjosh May 25 '20 at 19:49
  • Does this answer your question? [How to store a dataframe using Pandas](https://stackoverflow.com/questions/17098654/how-to-store-a-dataframe-using-pandas) – Błotosmętek May 25 '20 at 19:53
  • Did you made any changes after importing in the dataframe? Else you can just import every time you want to use. – sam May 25 '20 at 19:57
  • @roganjosh I just want it so that once the data from the file is imported and i convert the script to .exe there should not be any other files just the .exe . Is that possible? – Slartibartfast May 25 '20 at 20:13
  • @PrateekDewan Thanks for your suggestion. I am looking into pickel, it does solve my biggest problem that the contents are not visible in the pickel file. – Slartibartfast May 25 '20 at 20:16

2 Answers2

1

[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.

sam
  • 2,263
  • 22
  • 34
  • How, exactly, does this differ from the file that they want to now delete? You may have made it more universal, but the exact same circumstance exists... they have a file – roganjosh May 25 '20 at 19:51
  • If no changes made in the excel content then it is no different. I believe there might be several operations made on the dataframe which the user wants to save and not do again. Otherwise, I do not see the purpose of the question – sam May 25 '20 at 19:55
  • I'm not clear on the purpose of the question, either. But if they can parse an Excel file in, then they can also just save it back as an Excel file. And overwrite existing content if things change. It's no different to a CSV – roganjosh May 25 '20 at 19:57
  • @sam thanks for your answer. My issue with saving it to csv or excel file in my case it that my script has multiple csv and excel files and thus once i make a .exe file i dont want to also have to deal with multiple excel or csv files and want just the .exe file. To that end i wanted to know if it is possible to store the data from excel file in the code or someway where i just have an .exe file. – Slartibartfast May 25 '20 at 20:10
  • 1
    Ahhh.. Okay got the purpose. Then a look_up table like a dictionary could be the way if you have a small dataframe. – sam May 25 '20 at 20:16
  • 1
    Updated the answer, see if you find it useful – sam May 25 '20 at 20:19
1

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.

Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6