How do I manually create a dicitionary of series in python?

I want to create a dictionary that has two series (manually). The series needs an index and a value. I don't know how to add the index.
This is what I want:

enter image description here

Here is what I have currently:

dict_full_profile = {}
dict_full_profile['MALE_EDU'] = {}
dict_full_profile['MALE_EDU'] = pd.Series([0.00428,0.184705,0.202432,0.300256,0.109422432,.177827355,0.0210740250])
dict_full_profile['MALE_EMP'] = pd.Series([0.017017233, 0.38140,0.300256,0.2749405,0.0263854])

How do I add the string indices?

3 answers

  • answered 2021-04-21 15:29 Jonathan Hay

    To create a dictionary manually you need to create an empty dictionary as well as any embedded levels. To create an index just use the syntax below (eg ,index[]).

    dict_full_profile = {}
    dict_full_profile['MALE_EDU'] = {}
    dict_full_profile['MALE_EDU'] = pd.Series([0.00428,0.184705,0.202432,0.300256,0.109422432,.177827355,0.0210740250],index=['G', 'F', 'E', 'D', 'C', 'B', 'A'])
    dict_full_profile['MALE_EMP'] = pd.Series([0.017017233, 0.38140,0.300256,0.2749405,0.0263854],index=['AA', 'AF', 'AE', 'AD', 'AC'])
    

  • answered 2021-04-21 15:32 Maria Noaptes

    You can try doing something like

    dict_full_profile = {}
    dict_full_profile['MALE_EDU'] = {"Grade school":0.00428,"Some college" :0.184705, ...}
    

    But if you want to keep your data in a pd.Series collection you can keep your code as it is and make a separate dictionary with the indexes.

    indexes= {}
    indexes['MALE_EDU'] = ["Grade school", "Some college", ...]
    

    The order of the indexes in the new dictionary will be the same as in the series, so you can make something like dict_full_profile['MALE_EDU'][indexes['MALE EDU'].indexOf("Grade school")] to get the corresponding value

  • answered 2021-04-21 16:30 Maria Noaptes

    import pandas as pd
    
    dict_full_profile = {}
    dict_full_profile['MALE_EDU'] = {}
    dict_full_profile['MALE_EDU'] = pd.Series([0.00428,0.184705,0.202432,0.300256,0.109422432,.177827355,0.0210740250])
    dict_full_profile['MALE_EMP'] = pd.Series([0.017017233, 0.38140,0.300256,0.2749405,0.0263854])
    
    indexes= {}
    indexes['MALE_EDU'] = ["Grade school", "Some college"]
    
    index = indexes['MALE_EDU'].index("Grade school")
    print(dict_full_profile['MALE_EDU'][index])
    
    index = indexes['MALE_EDU'].index("Some college")
    print(dict_full_profile['MALE_EDU'][index])
    

    whenever you want some number from a series in your initial dictionary, you consult the second one to see what is the corresponding index