
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
Replace values from a dataframe based on conditions
If you want to replace values from a dataframe based on conditions
โ You can use the built-in ๐บ๐ฎ๐๐ธ() function from Pandas.
Below is an illustration ๐ก

Apply colors to your Pandas dataframe
Have you ever wanted to quickly find some information JUST by looking at your dataframeโ
Things like:
โจ Which values are negative in each column?
โจ What is the maximum or the minimum value of each column?
โจ Which values are below or above the average?
The list goes on and on…
A great way of viewing such information is by using colors ๐จ
โ ๐ฃ๐ฎ๐ป๐ฑ๐ฎ๐.๐๐๐๐น๐ฒ is a built-in module that provides a high-level interface for styling your dataframe.

Here is a video illustration!
Print Pandas dataframe in Markdown
It is always better to print your data frame in a way that makes it easier to understand.
โ One way of doing that is to render it in a markdown format using the .๐๐_๐๐๐๐๐๐๐ ๐() function.
Let me know in the comment which one is your favorite.
โจ With Markdown โ or without markdown โ

SQL-like queries through dataframe
Pandas’ power can’t be explored enough in Data Science๐ป๐
As Data Analyst or Scientist, you might want to filter ๐ through your data to find relevant insights.
โ This can be achieved using the built-in ๐พ๐๐ฒ๐ฟ๐() function in Pandas.
It runs queries based on boolean expressions, as you would write a natural language sentence! ๐ฌ
Below is an illustration ๐

Transform Scikit Learn Processing to Pandas dataframe
If you have been taking a closer look ๐ง at Scikit learn preprocessing module, you might have noticed that the underlying functions return a numpy array ๐ข .
This can make it difficult to keep track of the original names of the features in the data.
Wouldn’t it be nice to have a Pandas ๐ผ dataframe instead without any additional lines of code to keep those features’ names?
โ This can be achieved using the ๐๐ฒ๐_๐ผ๐๐๐ฝ๐๐ API from the new version (1.2) of Scikit Learn.
Below is an illustration ๐ก

Extract periods from the Datetime column
Days, weeks, months, or quarters ๐ , ….. Each one can play an important role depending on the tasks at hand.
โ With the ๐๐ผ_๐ฝ๐ฒ๐ฟ๐ถ๐ผ๐ฑ() function, you can extract from the date column each of such relevant information.
Below is an illustration ๐ก

Python
Number of elements in a list
Still using loops ๐ to determine how often each item occurs in a list?
Maybe there is a better and much more elegant Pythonic ๐ way!
โ You can use the ๐๐ผ๐๐ป๐๐ฒ๐ฟ class from Python to compute the counts of the elements in a list.
Below is an illustration ๐ก

Combine elements from multiple lists
Are you trying to aggregate elements from multiple lists?
โ Stop using ๐ณ๐ผ๐ฟ loops ๐ and adopt the following approach.
โ The Python built-in ๐๐ถ๐ฝ() function.
Below is an illustration ๐ก

Create multiple lists from aggregated elements
When trying to aggregate elements from multiple lists, the most elegant and Pythonic way is to use the built-in ๐๐ถ๐ฝ() function.
Now, what if you want to proceed the other way around: create multiple lists from those aggregated elementsโ
โ Forget ๐ณ๐ผ๐ฟ loops ๐
โ Just combine the ๐๐ถ๐ฝ() function with ๐ฎ๐๐๐ฒ๐ฟ๐ถ๐๐ธ *
Below is an illustration ๐ก

I am a big fan of list comprehension
Don’t just code in Python like most people, take the shortcut and the most efficient approach.
Imagine that you want to create a list with only even numbers from an existing one. The most obvious idea is using a "for" loop. But the most elegant one is using a list comprehension, which is more compact, simpler, and easier to debug.
Below is an illustration ๐ก

Where there is list comprehension there is a dictionary comprehension
Similarly to list comprehension, it is also possible to create dictionary comprehension. It also provides the same benefits compared to list comprehension.
Let’s consider having a dictionary where the keys are the index and the values are the actual numbers from the original list with the constraint of being an even number.
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