In [1]:

```
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (15, 3)
plt.rcParams['font.family'] = 'sans-serif'
```

We saw earlier that pandas is really good at dealing with dates. It is also amazing with strings! We're going to go back to our weather data from Chapter 5, here.

In [9]:

```
weather_2012 = pd.read_csv('../data/weather_2012.csv', parse_dates=True, index_col='Date/Time')
weather_2012[:5]
```

Out[9]:

You'll see that the 'Weather' column has a text description of the weather that was going on each hour. We'll assume it's snowing if the text description contains "Snow".

pandas provides vectorized string functions, to make it easy to operate on columns containing text. There are some great examples in the documentation.

In [10]:

```
weather_description = weather_2012['Weather']
is_snowing = weather_description.str.contains('Snow')
```

This gives us a binary vector, which is a bit hard to look at, so we'll plot it.

In [4]:

```
# Not super useful
is_snowing[:5]
```

Out[4]:

In [13]:

```
# More useful!
is_snowing=is_snowing.astype(float)
is_snowing.plot()
```

Out[13]:

If we wanted the median temperature each month, we could use the `resample()`

method like this:

In [14]:

```
weather_2012['Temp (C)'].resample('M').apply(np.median).plot(kind='bar')
```

Out[14]:

Unsurprisingly, July and August are the warmest.

So we can think of snowiness as being a bunch of 1s and 0s instead of `True`

s and `False`

s:

In [15]:

```
is_snowing.astype(float)[:10]
```

Out[15]:

and then use `resample`

to find the percentage of time it was snowing each month

In [16]:

```
is_snowing.astype(float).resample('M').apply(np.mean)
```

Out[16]:

In [17]:

```
is_snowing.astype(float).resample('M').apply(np.mean).plot(kind='bar')
```

Out[17]:

So now we know! In 2012, December was the snowiest month. Also, this graph suggests something that I feel -- it starts snowing pretty abruptly in November, and then tapers off slowly and takes a long time to stop, with the last snow usually being in April or May.

We can also combine these two statistics (temperature, and snowiness) into one dataframe and plot them together:

In [18]:

```
temperature = weather_2012['Temp (C)'].resample('M').apply(np.median)
is_snowing = weather_2012['Weather'].str.contains('Snow')
snowiness = is_snowing.astype(float).resample('M').apply(np.mean)
# Name the columns
temperature.name = "Temperature"
snowiness.name = "Snowiness"
```

We'll use `concat`

again to combine the two statistics into a single dataframe.

In [19]:

```
stats = pd.concat([temperature, snowiness], axis=1)
stats
```

Out[19]:

In [20]:

```
stats.plot(kind='bar')
```

Out[20]:

Uh, that didn't work so well because the scale was wrong. We can do better by plotting them on two separate graphs:

In [21]:

```
stats.plot(kind='bar', subplots=True, figsize=(15, 10))
```

Out[21]: