Dictionaries in Python

Lately, I’ve been learning how to take advantage of dictionaries in Python to store and organize information. Dictionaries have a lot of different purposes in Python, but as it applies to data visualization, it can be extremely helpful. Simply put, dictionaries allow us to store almost limitless amounts of information about whatever we want, and then call it when we’re writing our programs or creating visualizations. Doing this allows us to model real-world information more accurately, whether it is storing information about a person’s information, statistics about sports, econometric data, and so on. Another beautiful thing about dictionaries is that they’re easy to manipulate. Rather than using variables for everything, we can sort, loop, or write functions using dictionary values. We can even create other lists of dictionaries. The example below shows how we can create a dictionary where the values are lists, and then cycle through the lists to create messages about each person’s favorite language. Yes, I realize it’s a bit weird just to post a screenshot of my code. However, it seems like a pretty intense process to show an iPython notebook in WordPress. (Edit: Updated the post to show iPython notebook)


<script type="text/javascript">
function resizeIframe(ifrm) {       
    ifrm.style.height = ifrm.contentWindow.document.body.scrollHeight + 'px';
    // Setting the width here, or setting overflowX to "hidden" as above both 
    // work for this page. It may be that one turns out to be better.
    ifrm.style.width = ifrm.contentWindow.document.body.scrollWidth + 'px';
}
</script>

<script type="text/javascript">
function onLoad() {    
    var ifrm = document.getElementById('ipython_notebook_frame');   
    setTimeout(resizeIframe, 0, ifrm);
}
</script>

<iframe id="ipython_notebook_frame" style="height: 500px; width: 100%; padding: 0; border: none;" src="http://grantaweber.com/wp-content/uploads/2017/03/Dictionaries2.html" width="300" height="150">
</iframe>

<script type="text/javascript">
// By deleting and reinstating the iframe src, and by using setTimeout
// rather than resizing directly we convince Safari to render the
// page. See http://www.bitsofbits.com/wp-content/uploads/2015/01/custom.css
var iframe = document.getElementById('ipython_notebook_frame');
iframe.onload = onLoad;
var iSrc = iframe.src;
iframe.src = '';
iframe.src = iSrc;
</script>

Another example of using dictionaries is the basketball data I analyzed for an introductory course I took. The course was called “Python A-Z: Python for Data Science” and I found it through Udemy. We stored each player’s statistics for the past five years into dictionaries and then were able to call upon them way further down the script to run some analysis. Below you’ll see just one of the visualizations I was able to create using dictionaries for organizing the data, NumPy for the statistical functions we needed to run, and Matplotlib for the visualization part. Watch out for another post on Matplotlib and Seaborn shortly.


<script type="text/javascript">
function resizeIframe(ifrm) {       
    ifrm.style.height = ifrm.contentWindow.document.body.scrollHeight + 'px';
    // Setting the width here, or setting overflowX to "hidden" as above both 
    // work for this page. It may be that one turns out to be better.
    ifrm.style.width = ifrm.contentWindow.document.body.scrollWidth + 'px';
}
</script>

<script type="text/javascript">
function onLoad() {    
    var ifrm = document.getElementById('ipython_notebook_frame');   
    setTimeout(resizeIframe, 0, ifrm);
}
</script>

<iframe id="ipython_notebook_frame" style="height: 500px; width: 100%; padding: 0; border: none;" src="http://grantaweber.com/wp-content/uploads/2017/03/BasketballData2.html" width="300" height="150">
</iframe>

<script type="text/javascript">
// By deleting and reinstating the iframe src, and by using setTimeout
// rather than resizing directly we convince Safari to render the
// page. See http://www.bitsofbits.com/wp-content/uploads/2015/01/custom.css
var iframe = document.getElementById('ipython_notebook_frame');
iframe.onload = onLoad;
var iSrc = iframe.src;
iframe.src = '';
iframe.src = iSrc;
</script>

Please follow and like us: