# Python Pandas For Your Grandpa - 4.8 Challenge: Product Volumes

Contents

## Setup

Given a Series of product descriptions like “birch table measures 3’x6’x2'”, estimate the volume of each product. (Note that 3' means 3 feet.)

``````import numpy as np
import pandas as pd

descriptions = pd.Series([
"soft and fuzzy teddy bear, product dims: 1'x2'x1', shipping not included",
"birch table measures 3'x6'x2'",
"tortilla blanket ~ sleep like a fajita ~ 6'x8'x1'",
"inflatable arm tube man | 12'x1'x1' when inflated",
"dinosaur costume -- 6'x4'x2' -- for kids and small adults"
], dtype='string')
``````

## Solution 1

I came up with two solutions to this one, both of which use regular expressions. Now, even though we didn’t cover regular expressions in the course, I included this problem because once you do learn regular expressions, you’ll wanna know how to use them with pandas. And, this’ll give you a good chance to see how they’re useful.

``````dims = descriptions.str.extract(r"(\d+)'x(\d+)'x(\d+)'").astype('int64')
dims.product(axis=1)
## 0     2
## 1    36
## 2    48
## 3    12
## 4    48
## dtype: int64
``````

## Solution 2

``````dims = descriptions.str.extractall(r"(\d+)'").astype('int64')
dims.groupby(dims.index.get_level_values(0)).agg(pd.Series.product)
##     0
## 0   2
## 1  36
## 2  48
## 3  12
## 4  48
``````