NBA Finals and Pandas

The NBA season ended last night as the Thunder beat the Pacers 103–91 in the seventh game of the Finals. I read this morning that this was the 20th Finals to go seven games in the 79-year history of the NBA, and I wondered what the distribution of game counts was.

I found all the Finals results on this Wikipedia page in a table that starts out this way:

NBA Finals table excerpt from Wikipedia

I figured I’d use what I learned about how Pandas can read HTML tables to extract the information I wanted. So I started an interactive Python session that went like this:

python:
>>> import pandas as pd
>>> dfs = pd.read_html('https://en.wikipedia.org/wiki/List_of_NBA_champions')
>>> df = pd.DataFrame({'Result': dfs[2]['Result']})
>>> df[['West', 'East']] = df.Result.str.split('–', expand=True).astype(int)
>>> df
     Result  West  East
0     1–4     1     4
1     4–2     4     2
2     4–2     4     2
3     4–2     4     2
4     4–3     4     3
..    ...   ...   ...
74    2–4     2     4
75    4–2     4     2
76    4–1     4     1
77    1–4     1     4
78    4–3     4     3

[79 rows x 3 columns]
>>> df['Games'] = df.West + df.East
>>> for n in range(4, 8):
...     print(f'{n}  {len(df[df.Games == n])}')
...     
4  9
5  20
6  30
7  20

OK, you’re right, this is an edited version of the session. The command that created the West and East fields took a few tries to get right. Also, I’m pretty sure there’s a better way to put the Results column from the original table into its own dataframe, but creating an intermediate dictionary was the first thing that came to mind. Overall, I was pleased that I didn’t need to do much thrashing about; I’ve used Pandas long enough now to get most things right or nearly right on the first try.

The upshot is this:

Games Count
4 9
5 20
6 30
7 20

I expected the 4–0 series to be the least common. As a Bulls fan, I suppose I should have guessed that 4–2 series are the most common—Michael Jordan was responsible for five of them.

If next year’s Finals is a sweep, I will definitely do this again. What a neat and tidy table that will be.