what is a more elegible way to store and retrieve the values from a dict?

I would recommend you to use Pandas for this. You can use pandas dataframes to manage data like this with ultimate flexibilty:

import numpy as np
import pandas as pd

dict_score = {
'Mike': 100,
'John':98,
'HuxiaoMing':78,
'Mechele':66}

dict_sex = {
'Mike': "male",
'John':"male",
'HuxiaoMing':"female",
'Mechele':"male"}

dict_height = {
'Mike': 170,
'John':198,
'HuxiaoMing':178,
'Mechele':166}

df = pd.DataFrame({'Score': dict_score,'Sex': dict_sex, 'Height': dict_height}).reset_index()

Here our dataframe (df) will look like:

        index  Score     Sex  Height
0        Mike    100    male     170
1        John     98    male     198
2  HuxiaoMing     78  female     178
3     Mechele     66    male     166

Now you can easily apply the get_score function and create new column like this:

df['Degree'] = df['Score'].apply(get_degree)

Here now our df will look like:

        index  Score     Sex  Height  Degree
0        Mike    100    male     170    good
1        John     98    male     198    good
2  HuxiaoMing     78  female     178  normal
3     Mechele     66    male     166  normal

Let’s work more!

To get any column (Each columns are pandas series) use like:

df['COLUMN_NAME']

Now like we want to get average of the scores of the students:

df['Score'].average()

Get descriptive statistics:

df['Score'].describe()

And using .apply() you can apply your own functions with powerful combinations of lambda.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top