Pandas with Python 2.7 Part 9 - Statistical Information
Skills:
ML Maths Basics85%
Key Takeaways
This video tutorial covers the use of pandas with Python 2.7 to perform statistical operations such as calculating variance, correlation, and covariance on a dataset, with applications in data analysis and investing.
Full Transcript
what is going on everybody Welcome to the ninth pandas and python Basics tutorial video in this video we're going to be talking about uh some statistics Gathering things so the last thing we did was um standard deviation but there's also a whole slew of other things that we can do uh so I'm just going to show you a few more things as far as Gathering statistics about the data that you're looking at so with that let's go ahead and get started so uh let's do I guess we can keep High minus low but the rest of this stuff let's just go ahead and delete and we'll make some space that's good enough so the first thing that we can do is we can use uh do describe so we can say DF do describe like that and this is going to give us um some basic information like rows the average of all the rows and actually we need to print this out rows average of all the rows uh the minimum the maximum and some other stuff so anyway uh we come over here [Music] and this gives us data on all the columns basically so here we have the amount of rows so everybody has the same amount of rows okay so it's 2830 rows um this gives us the average of each row so the average of opens is 1172 highs and so on um the average volumes the average adjusted close blah blah blah the standard deviation for the whole set the minimum for the whole set um 25 50 75 and then the maximum for the whole set so those are like stages in between so the minimum the you know 75 or the 25th percentage number you know and all that kind of stuff so as far as you know with high minus or the high or the I'm trying to think how to describe this I'm having a very hard time if you couldn't if you couldn't tell what's the minimum number what is the you know uh bottom fourth number so on uh anyway enough on that um so just some basic numbers um on the on the whole data set so we'll close out of this uh the next thing that we can do is correlation so basically this is going to give us the correlation data so remember the like we plotted like a scatter chart and we want we kind of used a scatter chart to decide for ourselves if there was any sort of correlation between volume and then you know High minus low and all that kind of stuff so volume and volatility pretty much um so but we can actually find the correlation using a pretty simple query or command uh do core so we can go ahead and save and run that and that gives us correlation of pretty much everything here so this is correlation of open to open um you know so it's a one right it's perfect one as opposed to a perfect I don't think we have any perfect negative ones we don't even really have anything that's really not correlated well anyway it gives you the correlation so the correlation of say high to open they ought to be pretty close right so when one changes significantly probably the other one did too so that makes a whole lot of sense what about volume volume to high minus low right so here's volume and then High minus low so volume to high minus low point4 correlation so I mean it's somewhat correlated it's not zero but it's not a perfect one either but at least not a negative one so we can sort of deduce that a lot of times when volume is high the high amount is slow is also a large number um and yeah so anyway those are just some correlation statistics on all of the data in the data set so it's really quick you know if you wanted to calculate correlation yourself um or at least for me uh the processing time to do that because I would just use a for Loop and iterate through you know each one You' build it I mean I it would take a lot longer than the instantaneous number that we got there anyway uh the other thing we can do is covariant so CV um a lot of people have conf confusion about what is covariance and what is correlation um myself included I would say that at least my understanding of the difference is covariance is almost like a measure of the strength of the correlation like correlation is kind of like a looser thing you know they always say like correlation is not causation right whereas covariance is more like the strength of that correlation um so maybe it's a one step closer to causation but anyway uh a very confusing topic if you try to figure it out and if you ever look it up online like people are pretty much interchange the two whenever you're trying to find a really concrete answer what is covariance you'll find like an identical definition to what is correlation right so anyway um the way I understand it is like Co correlation is just kind of like a loose thing and then covariance is a little bit stronger and then covariance is like a strength of correlation but someone feel free to correct me if you want um anyway so there is core variance again this would take a lot longer to calculate yourself I'm pretty impressed on how fast uh pandas will calculate these two things um the other thing that we can do is we could say for example um we could do something like this so we could say print DF uh dot like if we want to just compare two right so we could say print uh DF um a list within it and we want to say what's the correlation let's say uh volume to um you know High minus the low and then do dot core um we'll go yeah save and run that and this gives us the correlation we already kind of picked this up uh before but but anyway obviously volume to volume is one and high high to low is you know. 39 or whatever now uh interestingly enough um it's surprising how fast this is and how builtin this is uh this is actually a very popular method of data analysis especially when we look at things like Forex but also Stock Investing and stuff um people make these massive correlation or Co variance tables just to find stocks that are correlated or um 4X ratios that are correlated with each other and then that way when one reacts first they invest immediately in the other one because they're they're expecting that one to react with it and this actually is a fairly decent strategy that people employ um and people pay actually a good amount of money for services that do this for them there's websites that like sell just this um so I think it's kind of comical that it's like built right into pandas and it's pretty simple to do I mean I think that anybody who's never programmed before could figure this one out pretty quick and save themselves uh paying someone else to do this but anyway for example what we could do is and nobody has to follow me exactly with this uh I think probably what I'll do is I'll just copy and paste some data into here and then I will link to the copy and paste just because otherwise it will take a really long time to do this so first off let's go ahead do we already have yeah we got dat time imported and all that so let me copy and paste this and I'll explain what uh what exactly I'm doing here yeah this will work I suppose okay so we'll copy this and we'll come over here paste and so what we have going on here is we start here so here we're pulling just a bunch of data from Yahoo finance right not really that much actually but a bunch of data uh we're pulling data for City Group Apple Microsoft Tesla Bank of America Blackberry Chipotle eBay JP Morgan Starbucks Target and Wells Fargo and then what we're doing was we're printing the head of C uh just so you can see it what what we've done this might not be the most pythonic way of doing what I'm about to do but the reason why I'm doing this is we take C strip away everything except for adjusted close um so we're deleting all the other columns then we're saying core comp um so this is a compilation of correlation equals c and then what we're doing is we're renaming The Columns so what remains was the adjusted cloes for City group so I'm just renaming that to see in plays true and then now we're saying the column of core comp for Bank of America equals Bank of America's adjusted close column and so on we're going all the way through so now what we've got is for core comp we've got a bunch of columns here that are strictly stocks uh and adjusted close for those stocks now some people want to know like what what's the point of adjusted close versus close most of the time 99% of the time it's not going to matter but what adjusted close is going to do for you is if there's like a stock split or something like that it's going to adjust for you instead of having some wild crazy number there so we use adjusted close um for that purpose now uh moving on so we're going to do all of these then we print the header of that file and then we're going to print out core comp. and that's going to be the correlation for all of these companies so chances are Bank of America uh JP Morgan and Wells Fargo are going to have a slightly you know pretty high correlation probably above s um you know Blackberry uh I don't know whose Blackberry is going to be correlated with here but I'm not really sure these other ones maybe Tesla I don't know is there techy stocks but I don't know uh anyway let's go ahead and run it and see what happens so um save continue okay it's going to take okay took a second to grab the data but we got the data so obviously City Group City Group perfectly correlated Bank of America to City Group 97 almost a perfect correlation um no surprise big Banks um interestingly enough Microsoft to City Group possibly just because they the S&P 500 together uh quite a few close correlations but then we see like Blackberry to City Group negative Blackberry to Bank of America negative Blackberry to Microsoft which is a surprising negative um I would expect that to at least have been somewhat positive um pretty much Blackberry to everyone is negative so far that's kind of interesting Blackberry to everyone is just negative um I forget what our time frame is here but probably because Blackberry has been sucking lately what we got 2011 to 2014 yeah that's probably why my's just been going downhill I think uh so that's probably why but JP Morgan is City Group again very positive very high correlation um really so correl see Target and chipotle kind of a weak correlation well as far trying to find some stuff that isn't with Blackberry Wells Fargo to Appo apple apple um you know so anyway what people do is they take massive tables like this like really they would probably take the entire Russell 3,000 or even even more and compare them while and find really kind of obscure connections um not really sure that makes the best sense to do that I think you still need to have a logical because again correlation is not causation and if you search for it you'll find plenty of stocks that are apparently correlated but not correlated but people do it anyways um especially with Forex um and sometimes Forex it's better you know if you look at it with human eyeballs and then you'd be like oh I guess that would be correlated or something but uh anyway like I said people pay a kind of a lot of money uh for this and you do it right here within Python and pandas so anyways thought I'd show you guys that real quick if I forget to put a link to this uh someone remind me uh so you can play with it and you change up the names you could use um Forex ratio symbols instead and all that so anyway uh that's going to conclude this tutorial if anybody has any questions or comments feel free to leave them below as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
In this tutorial, we cover the various statistical operations that Pandas can quickly perform on your dataset. This includes things like variance and correlation. This is actually quite impressive, as a popular method of investing is to find correlated assets and invest in the ones lagging change. Many people pay for a service to do this for them, yet it is actually relatively simple for us to program with Pandas.
Sample code for the series: http://pythonprogramming.net/python-2-7-pandas-data-analysis/
Pandas tutorial series: https://www.youtube.com/playlist?list=PLQVvvaa0QuDfHt4XU7vTm22xDegR0v0fQ
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: ML Maths Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI