Dunder Data Challenge #5 — Keeping Values Within the Interquartile Range

dunder data challenges Nov 14, 2019

In this challenge, you are given a table of closing stock prices for 10 different stocks with data going back as far as 1999. For each stock, calculate the interquartile range (IQR). Return a DataFrame that satisfies the following conditions:

  • Keep values as they are if they are within the IQR
  • For values lower than the first quartile, make them equal equal to the exact value of the first quartile
  • For values higher than the third quartile, make them equal equal to the exact value of the third quartile

Start this challenge in a Jupyter Notebook right now thanks to Binder (mybinder.org)

import pandas as pd
stocks = pd.read_csv('../data/stocks10.csv', index_col='date', parse_dates=['date'])
stocks.head()

Challenge

There is a straightforward solution that completes this challenge in a single line of readable code. Can you find it?

Become a pandas expert

If you are looking to completely master the pandas library and become a trusted expert for doing data science work, check out my book Master Data Analysis with Python. It comes with over 300 exercises with detailed solutions covering the pandas library in-depth.

Close

Register for a free account

Upon registration, you'll get access to the following free courses:

  • Python Installation
  • Intro to Jupyter Notebooks
  • Intro to Pandas
  • Python  and Pandas Challenges