Contents

Python Pandas For Your Grandpa - 5.2 Challenge: Pickle

Setup

Given a Series called pickle, replace NaNs using the following algorithm.

For each NaN:

  1. get the nearest non-NaN value before and after it
  2. replace NaN with the minimum of those two non-NaN values

For NaNs on or near the endpoints of the Series, use the closest non-NaN value.

import numpy as np
import pandas as pd

pickle = pd.Series([1.5, np.nan, 2.3, np.nan, np.nan, -3.9, np.nan, 4.5, np.nan, np.nan, np.nan, 1.9, np.nan])
print(pickle)
## 0     1.5
## 1     NaN
## 2     2.3
## 3     NaN
## 4     NaN
## 5    -3.9
## 6     NaN
## 7     4.5
## 8     NaN
## 9     NaN
## 10    NaN
## 11    1.9
## 12    NaN
## dtype: float64

Solution

prevs = pickle.ffill()
nexts = pickle.bfill()
pickle.fillna(pd.concat([prevs, nexts], axis=1).min(axis=1))
## 0     1.5
## 1     1.5
## 2     2.3
## 3    -3.9
## 4    -3.9
## 5    -3.9
## 6    -3.9
## 7     4.5
## 8     1.9
## 9     1.9
## 10    1.9
## 11    1.9
## 12    1.9
## dtype: float64

Course Curriculum

  1. Introduction
    1.1 Introduction
  2. Series
    2.1 Series Creation
    2.2 Series Basic Indexing
    2.3 Series Basic Operations
    2.4 Series Boolean Indexing
    2.5 Series Missing Values
    2.6 Series Vectorization
    2.7 Series apply()
    2.8 Series View vs Copy
    2.9 Challenge: Baby Names
    2.10 Challenge: Bees Knees
    2.11 Challenge: Car Shopping
    2.12 Challenge: Price Gouging
    2.13 Challenge: Fair Teams
  3. DataFrame
    3.1 DataFrame Creation
    3.2 DataFrame To And From CSV
    3.3 DataFrame Basic Indexing
    3.4 DataFrame Basic Operations
    3.5 DataFrame apply()
    3.6 DataFrame View vs Copy
    3.7 DataFrame merge()
    3.8 DataFrame Aggregation
    3.9 DataFrame groupby()
    3.10 Challenge: Hobbies
    3.11 Challenge: Party Time
    3.12 Challenge: Vending Machines
    3.13 Challenge: Cradle Robbers
    3.14 Challenge: Pot Holes
  4. Advanced
    4.1 Strings
    4.2 Dates And Times
    4.3 Categoricals
    4.4 MultiIndex
    4.5 DataFrame Reshaping
    4.6 Challenge: Class Transitions
    4.7 Challenge: Rose Thorn
    4.8 Challenge: Product Volumes
    4.9 Challenge: Session Groups
    4.10 Challenge: OB-GYM
  5. Final Boss
    5.1 Challenge: COVID Tracing
    5.2 Challenge: Pickle
    5.3 Challenge: TV Commercials
    5.4 Challenge: Family IQ
    5.5 Challenge: Concerts