Python: Learning Data Frames, Dictionaries and Objects ver. 2

Last Updated on August 6, 2022 by shibatau

Let’s create a simple but practical data set and deal with it using data frames, dictionaries and objects. We will learn how differently they handle the data.

I. Using Data Frames

II. Using Dictionaries

III. Using Objects

# Using .rank() with Subclass
class Comparison():

    def __init__(self, id, group, english, math):
        self.id = id
        self.group = group
        self.english = english
        self.math = math
        self.rank = 0
        self.total = self.english + self.math
        self.average = (self.english + self.math)/2

    def all(self):
        print(f"Your average is {self.average}.")
        
class Comparisons(dict):

    def __init__(self):
        super(Comparisons, self).__init__()

    def rank(self):
        sorted_comparisons = sorted(self.values(), key=lambda c: c.average, reverse=True)
        for rank, comparison in enumerate(sorted_comparisons, 1):
            comparison.rank = rank

We will create Subclasses to use .rank( ). See more information here:

What is the best approach to rank a dictionary of objects?

IV. The Scripts

You can see the scripts here though they are not completed.

https://colab.research.google.com/drive/1NWAjZxkul284ih3cg2QhIu5Xm6F8JUj2?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.