Pandas and Method Chaining, Meme Stock

Let's look at how we can use Pandas' method chaining to solve a problem.

Python Exercise of The Week

The Dataset

The dataset we will practice with is coming from the Singapore Government Public Data. You can download it with the link below: 

Extract from the dataset

Pandas: The Python Library

Pandas is a Python open source library that is commonly used for data analysis and machine learning tasks. It provides data structures and operations for working with numerical tables and time series.

You can find more information about it here.

To install it, you can open a notebook in JupyterLab or Notebook for example and type:

The Use Case

Display the total of mobile subscriptions by year and per type of plan in descending order

Before looking at my solution, please try to practice on your own and see what solution you come up with!

Here is my code to answer this question

Python Code Snippet #1

Output

First 10 entries from the code

Comments

  • This code was created using method chaining. It means that it uses various methods that can be chained together and processed successively. I prefer to write code in this manner because I find it easier to write and read for several reasons:

    • It eliminates the need to create and mentally track temporary variables.

    • You write and read the code step by step, which allows you to understand its logic.

    • Debugging is simple: simply comment out operations that don't work until you find one that does, and then resume stepping forward.

  • In method chains, pipe includes user-defined methods.

  • assign adds a new column to a DataFrame in a chain

  • Using pipe and assign, we can create a new column 'year' that extracts the year from the month column.

  • groupby, agg, and sort_values help us to answer the question by grouping and summing the number of subscriptions in descending order by year and by type of plan

Finance Word of The Week: Meme Stock

You've probably heard of memes, which are popular humorous images that spread quickly on the internet. Here is one absolutely not related to this newsletter :)

The stock market is no exception, with its own "meme stock." It's a stock that has developed a cult-like following on social media platforms.

Meme stocks appear to be overpriced in comparison to their fundamentals. They remain highly-priced for long periods of time as meme stock community members keep their prices propped up. We've seen some past cases like GameStop or AMC. 

The Bed Bath & Beyond stock is a good recent example of this (BBBY). Let's take a look at its stock price history using the chart below:

Bed Bath & Beyond stock plunging

This plot is a candlestick chart. The candles represent the stock price increase (green) and decrease (red). The volume section is a bar chart that displays the stock's trading volumes. In my blog post here, you can learn more about this chart and how I plot it.

Volume and price show the typical stock rally starting around August 5th. Many retail investors saw a unique opportunity and decided to join it. Trading volumes peaked with a significant stock price increase on August 16th.

The BBBY stock price gained nearly 360% before plummeting when it was revealed that billionaire investor Ryan Cohen sold all his shares (and second largest investor in BBBY through his RC Ventures firm).

Ouch! You can imagine the consequences for the retail investors who joined this rally...

DISCLAIMER: None of this is financial advice. This newsletter is strictly educational and is not investment advice or a solicitation to buy or sell any assets or to make any financial decisions. Please be careful and do your own research.

That's a wrap for today. Stay curious, practice your Python and see you next week!

I have a blog on Medium: Khuong Lân Cao Thai – Medium

 If you find my posts helpful, you can consider donating to me the equivalent of a coffee with the button below.

Ko-fi doesn't take fees so all the money you donate will go straight to me. By supporting me, I will be able to invest more time in building content with higher quality