Publish AI, ML & data-science insights to a global community of data professionals.

Pandas & Python Tricks for Data Science & Data Analysis – Part 4

This is the fourth part of my Pandas & Python Tricks

Photo by Andrew Neel on Unsplash
Photo by Andrew Neel on Unsplash

Introduction

A couple of days ago, I shared some Python and Pandas tricks to help Data Analysts and Data Scientists quickly learn new valuable concepts that they might not be aware of. This is also part of the collection of tricks I share daily on LinkedIn.

Pandas

Change columns data type

Wrong data format is a common challenge when dealing with real-world ๐ŸŒ data.

For instance, you might have a numerical value that is stored as a string such as "34" instead of 34.

โœ… Using the astypefunction, you can easily convert data from one type to another (e.g. string to numerical).

Below is an illustration ๐Ÿ’ก

Check if two DataFrames are equal

Two columns with the same name may not contain the same values, and two rows with the same index may not be identical.

To know if two DataFrames are equal, you need to go deeper ๐Ÿ’ก to check if they have the same shape and same elements.

This is where the Pandas ๐—ฒ๐—พ๐˜‚๐—ฎ๐—น๐˜€() function comes in handy.

โœ… It returns True if the two DataFrames are equal.

โŒ It returns False if they are not equal.

Below is an illustration ๐Ÿš€

Python

Make your Python output more human-readable

Sometimes it is necessary to go beyond the default output provided by Python to make it more understandable by humans ๐Ÿ‘ฉ๐Ÿป โ€๐Ÿ’ผ ๐Ÿ‘ฉ๐Ÿฝ โ€๐Ÿ’ผ ๐Ÿ‘จ๐Ÿป โ€๐Ÿ’ผ ๐Ÿ‘จ๐Ÿฝ โ€๐Ÿ’ผ .

โœ… This can be achieved using the humanize library.

The full video tutorial is available here for more examples.

Convert natural language to numerical values

Natural language ๐Ÿ—ฃ ๏ธ is everywhere ๐ŸŒ , even in our DataFrames.

This is not a bad thing itself because it is the perfect ๐Ÿ‘๐Ÿ’ฏ type of data when performing natural language processing tasks.

However, their limitations ๐Ÿ‘Ž๐Ÿšซ become obvious when trying to perform numerical computation.

๐Ÿ›  ๏ธโœ… To tackle this issue, you can use the ๐—ป๐˜‚๐—บ๐—ฒ๐—ฟ๐—ถ๐˜‡๐—ฒ() function from the python library ๐—ป๐˜‚๐—บ๐—ฒ๐—ฟ๐—ถ๐˜‡๐—ฒ๐—ฟ.

โœจ It converts natural language expressions of numbers into their actual numerical values.

Below is an illustration ๐Ÿš€

Combine multiple lists

Using the + sign is probably the most common approach to combine ๐Ÿ”— lists. However, typing the + sign all the time can become easily boring when you have to deal with multiple lists.

โœ… Instead, you can use the add and reduce functions respectively from the operator and functools modules.

Below is an illustration ๐Ÿš€

Zip Iterables of different sizes

If you have been using the zip() function, then you might be aware of this limitation: it does not work with iterables of different sizes, which can lead to information loss.

๐Ÿ›  ๏ธโœ… You can tackle this issue with zip function’s cousin: zip_longest() function from the itertools module.

Instead of ignoring the remaining items, their values are replaced with None

That’s good, but can be even amazing using the fillvalue parameter to replace the None with a meaningful value.

Below is an illustration ๐Ÿš€

Conclusion

Thank you for reading! ๐ŸŽ‰ ๐Ÿพ

I hope you found this list of Python and Pandas tricks helpful! Keep an eye on here, because the content will be maintained with more tricks on a daily basis.

Also, If you like reading my stories and wish to support my writing, consider becoming a Medium member. With a $ 5-a-month commitment, you unlock unlimited access to stories on Medium.

Would you like to buy me a coffee โ˜•๏ธ? โ†’ Here you go!

Feel free to follow me on Medium, Twitter, and YouTube, or say Hi on LinkedIn. It is always a pleasure to discuss AI, ML, Data Science, NLP, and MLOps stuff!

Before you leave find the last two parts of this series below:

Pandas & Python Tricks for Data Science & Data Analysis – Part 1

Pandas & Python Tricks for Data Science & Data Analysis – Part 2

Pandas & Python Tricks for Data Science & Data Analysis – Part 3


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles