/logo-square.png

Python Pandas For Your Grandpa - 3.12 Challenge: Vending Machines

Setup You own a collection of vending machines and you want to evaluate your inventory. Given a DataFrame of (machine, product, stock), determine the number of machines carrying each product and how many of them are out of stock. In other words, build a new DataFrame with product as the row index, and calculated columns n_machines and n_machines_empty. import numpy as np import pandas as pd machine_products = pd.DataFrame({ 'machine': ['abc', 'abc', 'def', 'def', 'def', 'ghi'], 'product': ['skittles', 'soap', 'soap', 'm&ms', 'skittles', 'm&ms'], 'stock': [10, 0, 15, 2, 0, 3] }) print(machine_products) ## machine product stock ## 0 abc skittles 10 ## 1 abc soap 0 ## 2 def soap 15 ## 3 def m&ms 2 ## 4 def skittles 0 ## 5 ghi m&ms 3 Solution def count_zero(x): return (x == 0).

Python Pandas For Your Grandpa - 3.13 Challenge: Cradle Robbers

Setup Given a DataFrame of married couples and a separate DataFrame with each person’s age, identify “cradle robbers”, people who: are at least 20 years older than their spouse and who’s spouse is under the age of 30 import numpy as np import pandas as pd couples = pd.DataFrame({ 'person1': ['Cody', 'Dustin', 'Peter', 'Adam', 'Ryan', 'Brian', 'Jordan', 'Gregory'], 'person2': ['Sarah', 'Amber', 'Brianna', 'Caitlin', 'Rachel', 'Kristen', 'Alyssa', 'Morgan'] }).

Python Pandas For Your Grandpa - 3.14 Challenge: Pot Holes

Setup Fed up with your city’s roads, you go around collecting data on potholes in your area. Due to an unfortunate coffee spill, you lost bits and pieces of your data. So, given your DataFrame of pothole measurements, discard rows where more than half the values are NaN, otherwise impute NaNs with the average value per column unless the column is non-numeric, in which case use the mode. import numpy as np import pandas as pd potholes = pd.

Python Pandas For Your Grandpa - 3.2 DataFrame To And From CSV

In this section, we’ll see how you can read from and write to CSV files with Pandas. One of the most common ways to make a DataFrame is to load it from some pre-existing CSV file. Pandas has an awesome CSV reader for this, but before we use it, let’s make a DataFrame from scratch and write it to CSV so we have something to read. Here I’ll make a DataFrame called df with three columns and a thousand rows.

Python Pandas For Your Grandpa - 3.3 DataFrame Basic Indexing

In this section, we’ll look at some basic indexing operations for subsetting and modifying DataFrames. This is really similar to indexing a Series, so if you didn’t see my lecture on indexing a Series, you might wanna go back and watch that one. To start, let’s build a DataFrame called df with six rows and three columns. import numpy as np import pandas as pd df = pd.DataFrame({ 'shrimp': [10, 20, 30, 40, 50, 60], 'crab': [5, 10, 15, 20, 25, 30], 'red fish': [2, 3, 5, 7, 11, 13] }) print(df) ## shrimp crab red fish ## 0 10 5 2 ## 1 20 10 3 ## 2 30 15 5 ## 3 40 20 7 ## 4 50 25 11 ## 5 60 30 13 We can access an individual column in its Series form using either square brackets with the column name passed in

Python Pandas For Your Grandpa - 3.4 DataFrame Basic Operations

In this section, we’ll go over some basic DataFrame operations like how to insert or delete columns, and how to modify existing data. Inserting new columns into an existing DataFrame is easy. For example, if you have a DataFrame like this import numpy as np import pandas as pd df = pd.DataFrame({ 'a': [2, 3, 11, 13], 'b': ['fox', 'rabbit', 'hound', 'rabbit'] }) print(df) ## a b ## 0 2 fox ## 1 3 rabbit ## 2 11 hound ## 3 13 rabbit you can insert a new column, ‘c’, using df['c'] and setting it equal to either a list, Series, NumPy array, or a scalar.