Python Tricks for Keeping Track of Your Data
By: Khuyen Tran
Tags: Python Programming, Efficiency, Tech, Coding, Data Science

Python Tricks for Keeping Track of Your Data

How to keep track of information with a list, dictionary counter, and named tuple

Motivation

In your data science projects, sometimes you want to keep track of the information in your data or have the flexibility of updating the new input of data quickly and easily. Knowing how to use some of Python’s data objects will keep you stay organized and avoid mistakes while dealing with a huge amount of data in your data science career.

In this article, you will learn how to:

Keep track of index while looping

Update new dictionary items

Record new information with a reusable object

I will start with the problems and then go into the details of how to solve them with Python tools. I hope this approach will help you visualize where these tools could be applied in your code.

Photo by Edgar Chaparro on Unsplash

Keep Track while Looping

Let’s say you have a list of friends’ names. You want to loop through the list while keeping track of the count. How can you do that? This could be easily be done with enumerate

>>> friends = ['Ben', 'Kate', 'Thinh']

>>> for i, item in enumerate(friends):

>>> print(f'{i}: {item}')

0: Ben

1: Kate

2: Thinh

Or simply with dictionary comprehension

>>> {i: friends[i] for i in range(len(friends))}

{0: 'Ben', 1: 'Kate', 2: 'Thinh'}

Update New Dictionary Items:

You are using a dictionary to keep track of the words and their counts in the first sentence.

sent1 = {'love': 1, 'hate': 3}

But as you go to the second sentence, you want to update your old dictionary with the new sentence.

sent2 = {'love': 2, 'flower': 1}

So your new updated bag of words will become like this:

{'love': 3, 'hate': 3, 'flower': 1}

How could this be done? Wouldn’t it be nice to have some tools that allow you to do this easily? If that is what you are looking for, collections.Counter gets your back. collections.Counter the class allows elements in the set to have more than one occurrence

from collections import Counter

bag_words = Counter() sent1 = {'love': 1, 'hate': 3} bag_words.update(sent1) sent2 = {'love': 2, 'flower': 1} bag_words.update(sent2) bag_words

Outcome:

Counter({'love': 3, 'hate': 3, 'flower': 1})

Nice! Now you could easily update your bag of words as you collect more information from other sentences. To find how many unique words in the sentence, you could use len

>>> len(bag_words)

3

Or to find how total words in the sentence, you could use sum

>>> sum(bag_words.values())

7

Define Reusable Object with Namedtuple

You want to keep track of a list of information about your friends to prepare for their birthday. Since you don’t have that information available for now, you want first to create a place holder where you can input the information later. If you want to record Kate’s birthday, favorite food, color, and whether she is introverted, you could do like this:

>>> Kate = Friend('Feb', 'cake', 'pink', True)

and when you don’t remember her birthday, you could call

>>> Kate.birthday

'Feb'

The class object in Python will enable you to instantiate Kate, but you find it takes time to create a Friend class to hold simple information. If that is the case, namedtuple is your to-go function. nametuples allows you to define a reusable object for your records that ensure the correct filed names are used

from collections import namedtuple Friend = namedtuple('Friend' , 'birthday food color introvert') Kate = Friend('Feb', 'cake', 'pink', True) Ben = Friend('Jan', 'fish', 'red', False)

Display the information about Kate:

>>> Kate

Friend(birthday='Feb', food='cake', color='pink', introvert=True)

If you want to know if Ben is introverted or extroverted, you could call

>>> Ben.introvert

False

With nametuples , you could easily reuse the same object to instantiate new information.

Conclusion

Awesome! You have learned how to keep track of the information using enumerate , set comprehension, Counter , and namedtuple . I hope this tutorial gives you additional useful knowledge to add to your data science toolkits.

Feel free to fork and play with the code for this article in this Github repo.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: