Tuesday Tip #31: Data cleaning with pandas šŸ§¹


Hi Reader,

In two weeks, Iā€™ll be offering a Black Friday sale on ALL of my courses.

Iā€™ll send you the details next week!


šŸ”— Link of the week

ā€‹Easier data analysis with pandas (free)

This is my video series about pandas, the most popular Python library for data analysis. There are 30+ videos, most of which are designed for pandas beginners, though intermediate users can skip to the ā€œbonusā€ videos at the bottom.

Normally I wonā€™t link to my own tutorials in this section, but it seemed appropriate since the next few tips will be about pandas! šŸ¼


šŸ‘‰ Tip #31: Basic data cleaning operations with pandas

In last weekā€™s tip, I showed you three visualizations that I created from the World Happiness Report data: a world map, a line chart, and an arrow chart.

I created those visualizations using Datawrapper, a no-code tool that I highly recommend checking out!

As is the case with many Data Science projects, I spent most of my time on the data cleaning step. Once the data was clean, Datawrapper made the visualization step quite easy!

In this weekā€™s tip, Iā€™m going to show you how I transformed the raw data into the world map data using pandas. Here are the specific steps Iā€™ll demonstrate:

  1. Read in the data from Excel
  2. Change column names & round data
  3. Fix country names
  4. Filter data by year
  5. Write data to CSV file

If you want to follow along with the code, you can run it online using Google Colab.

Hereā€™s the end result:


Step 1: Read in the data from Excel

The dataset is stored online in an Excel sheet, so I read it directly from the URL using the read_excel function. (Note that I split the URL string into two lines by adding parentheses around it.)

To avoid storing unnecessary data, I only kept the three columns that were relevant for the analysis. Life Ladder is the average ā€œlife evaluationā€ reported by residents, on a scale from 0 to 10, and is the reportā€™s primary measure of happiness.


Step 2: Change column names & round data

I renamed the columns so that they are lowercase, have no spaces, and are more descriptive. Not only does this create consistency, but it also allows me to use dot notation instead of bracket notation for column selection.

I also rounded the happiness column to 2 digits using the round method (since thatā€™s enough precision for the purpose of this visualization), and I overwrote the existing happiness column with an assignment statement.


Step 3: Fix country names

First, I used the unique method to display all of the country names in the dataset.

However, not all of the country names used by the World Happiness Report (WHR) are the same as the ones used by Datawrapper. For example, the WHR uses Congo (Brazzaville) and Congo (Kinshasa), whereas Datawrapper uses Congo and Democratic Republic of Congo.

Second, I created a dictionary to map the WHR names to the Datawrapper names.

Finally, I used the replace method to update the country names, overwrote the existing column, and used the unique method to check that the operation worked.


Step 4: Filter data by year

I filtered the DataFrame using a condition so that only the rows from 2022 remained.


Step 5: Write data to CSV file

I wrote the contents of the DataFrame to a file using the to_csv method, and excluded the index from the CSV since itā€™s not meaningful.


Publish with Datawrapper

Once I uploaded the CSV into Datawrapper, it was easy to customize the mapā€™s appearance and publish it online.

Again, hereā€™s the end result, which you can click on and interact with:


If you enjoyed this weekā€™s tip, please forward it to a friend! Takes only a few seconds, and it really helps me reach more people! šŸ™

See you next Tuesday!

- Kevin

P.S. Ace your next job interview with zero knowledgeā€‹

Did someone awesome forward you this email? Sign up here to receive Data Science tips every week!

Learn Artificial Intelligence from Data School šŸ¤–

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School šŸ¤–

Hi Reader, The Python 14-Day Challenge starts tomorrow! Hope to see you there šŸ¤ž šŸ‘‰ Tuesday Tip: My top 5 sources for keeping up with AI I'll state the obvious: AI is moving incredibly FAST šŸ’Ø Here are the best sources I follow to keep up with the most important developments in Artificial Intelligence: The Neuron (daily newsletter) My top recommendation for a general audience. Itā€™s fun, informative, and well-written. It includes links to the latest AI news and tools, but the real goldmine is...

Hi Reader, Before todayā€™s tip, I wanted to give you a heads up: Tomorrow, Iā€™ll be launching something brand new! Watch out for the announcement šŸ‘€ šŸ‘‰ Tip #53: How to get great results from AI models through prompting In the year after ChatGPT was released, I remember noticing two new trends: Articles about ā€œprompt engineersā€ being hired for hundreds of thousands of dollars just to write prompts Endless guides promising to teach you the secrets of writing the perfect ChatGPT prompt My takeaway...

Hi Reader, Last week, I encouraged you to experiment with different LLMs, since thereā€™s no one model that is superior across all use cases. Specifically, I suggested you try using Chatbot Arena, which allows you to chat with multiple models at once. Itā€™s completely free, but has two significant disadvantages: Your chats are not private and may be used for research. It lacks the feature-rich interface provided by other LLMs. Today, I want to offer you a better method for experimenting with...