Using dictionaries stored in another jupyter notebook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ck25python
    New Member
    • Jan 2020
    • 20

    Using dictionaries stored in another jupyter notebook

    Hi There,

    I have thousands of dictionaries saved in one jupyter notebook (controlled by IT), and i would like to utitlize some of the dicitonies in to my jupyter notebook.Is there a way i can achieve this.

    Example: IT-Jupyter notebook contains below dictionary (just a sample):
    Code:
    dict_sample={'HR':'Human Resource','FIN':'Finance','LG':'Legal'}
    I would like to utilize above dictionary in to my jupyter notebook. Dummy df created below for reference.
    Code:
    data = {'name': ['John', 'Aaron', 'Anie'], 
            'Dept': ['HR', 'FIN', 'LG']}
    df = pd.DataFrame(data, columns = ['name','Dept'])
    
    df["Dept"]=df["Dept"].map(dict_sample)
    
    df
    Appriciate your advise here.
  • hussainmujtaba
    New Member
    • Apr 2020
    • 13

    #2
    You can use dump function of pickle in this way.First dump this dictionary in a pickle file.
    Code:
    import pickle
    
    a = {'HR':'Human Resource','FIN':'Finance','LG':'Legal'}
    
    with open('filename.pickle', 'wb') as handle:
        pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
    
    #Now you will have this file, take this to your PC , save it in same #folder as Jupiter notebook you are working in
    with open('filename.pickle', 'rb') as handle:
        b = pickle.load(handle)
    b contains your dictionary.
    You can also load data in pandas and then save it as csv file.
    Last edited by Rabbit; Sep 1 '20, 03:44 PM.

    Comment

    Working...