

{"id":23123,"date":"2018-07-29T04:00:36","date_gmt":"2018-07-29T04:00:36","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=23123"},"modified":"2026-04-29T17:16:08","modified_gmt":"2026-04-29T11:46:08","slug":"python-statistics","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-statistics\/","title":{"rendered":"Python Statistics &#8211; Python p-Value, Correlation, T-test, KS Test"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this <a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\" target=\"_blank\" rel=\"noopener\"><strong>Python<\/strong> <\/a>Statistics tutorial, we will learn how to calculate the p-value and Correlation in Python. Moreover, we will discuss the T-test and the KS Test with examples and code in Python Statistics.<\/span><\/p>\n<p>So, let&#8217;s start the Python Statistics Tutorial.<\/p>\n<h3><strong>p-value in Python Statistics<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">When talking about statistics, a p-value for a statistical model is the probability that, when the null hypothesis is true, the statistical summary is equal to or greater than the actual observed results. This is also termed \u2018<\/span><i><span style=\"font-weight: 400\">probability value<\/span><\/i><span style=\"font-weight: 400\">\u2019 or \u2018<\/span><i><span style=\"font-weight: 400\">asymptotic significance<\/span><\/i><span style=\"font-weight: 400\">\u2019.<\/span><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\" target=\"_blank\" rel=\"noopener\">Do you know about Python Decorators<\/a>?<\/strong><\/p>\n<p><span style=\"font-weight: 400\">The null hypothesis states that two measured phenomena experience no relationship to each other. We denote this as H or H<\/span><span style=\"font-weight: 400\">0<\/span><span style=\"font-weight: 400\">. One such null hypothesis can be that the number of hours spent in the office affects the amount of salary paid. For a significance level of 5%, if the p-value falls lower than 5%, the null hypothesis is invalidated. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Then it is discovered that the number of hours you spend in your office will not affect the amount of salary you will take home. Note that p-values can range from 0% to 100%, and we write them in decimals. A p-value for 5% will be 0.05.<\/span><\/p>\n<p><span style=\"font-weight: 400\">A smaller p-value bears more significance as it can tell you that the hypothesis may not explain the observation fairly. If one or more of these probabilities turn out to be less than or equal to <\/span><span style=\"font-weight: 400\">\u03b1, the level of significance, we reject the null hypothesis. For a true null hypothesis, p can take on any value between 0 and 1 with equal likelihood. For a true alternative hypothesis, p-values likely fall closer to 0.<\/span><\/p>\n<div id=\"attachment_23128\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pvalue.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23128\" class=\"wp-image-23128 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pvalue.png\" alt=\"Python Statistics\" width=\"400\" height=\"217\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pvalue.png 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pvalue-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pvalue-300x163.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-23128\" class=\"wp-caption-text\">Python p-Value<\/p><\/div>\n<h3><strong>T-test in Python Statistics<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Let\u2019s talk about T-tests. Such a test tells us whether a sample of numeric data strays or differs significantly from the population. It also talks about two samples- whether they\u2019re different. In other words, it gives us the probability of the difference between populations. The test involves a t-statistic. For small samples, we can use a T-test with two samples.<\/span><\/p>\n<div id=\"attachment_23142\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23142\" class=\"wp-image-23142 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python.png\" alt=\"Python statistics\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python.png 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python-300x157.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python-768x402.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/T-test-With-Python-1024x536.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-23142\" class=\"wp-caption-text\">Python Statistics &#8211; Python\u00a0T-test<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-generators\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s discuss\u00a0Python Generators<\/a><\/strong><\/p>\n<h4><strong>a. One-sample T-test with Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Let\u2019s try this on a single sample. The test will tell us whether the means of the sample and the population are different. Consider the voting populace in India and in Gujarat. Does the average age of Gujarati voters differ from that of the population? Let\u2019s find out.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import numpy as np\r\n&gt;&gt;&gt; import pandas as pd\r\n&gt;&gt;&gt; import scipy.stats as stats\r\n&gt;&gt;&gt; import matplotlib.pyplot as plt\r\n&gt;&gt;&gt; import math\r\n&gt;&gt;&gt; np.random.seed(6)\r\n&gt;&gt;&gt; population_ages1=stats.poisson.rvs(loc=18,mu=35,size=150000)\r\n&gt;&gt;&gt; population_ages2=stats.poisson.rvs(loc=18,mu=10,size=100000)\r\n&gt;&gt;&gt; population_ages=np.concatenate((population_ages1,population_ages2))\r\n&gt;&gt;&gt; gujarat_ages1=stats.poisson.rvs(loc=18,mu=30,size=30)\r\n&gt;&gt;&gt; gujarat_ages2=stats.poisson.rvs(loc=18,mu=10,size=20)\r\n&gt;&gt;&gt; gujarat_ages=np.concatenate((gujarat_ages1,gujarat_ages2))\r\n&gt;&gt;&gt; population_ages.mean()<\/pre>\n<p><strong><span style=\"font-family: Verdana, Geneva, sans-serif\">43.000112<\/span><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; gujarat_ages.mean()<\/pre>\n<p><strong>39.26<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.ttest_1samp(a=gujarat_ages,popmean=population_ages.mean())<\/pre>\n<p><strong>Ttest_1sampResult(statistic=-2.5742714883655027, pvalue=0.013118685425061678)<\/strong><br \/>\n<span style=\"font-weight: 400\">Now this value of -2.574 tells us how aberrant the sample mean is from the null hypothesis.<\/span><\/p>\n<h4><strong>b. Two-sample T-test With Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Such a test tells us whether two data samples have different means. Here, we take the null hypothesis that both groups have equal means. We don\u2019t need a known population parameter for this.<\/span><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/recursion-in-python\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s revise Recursion in Python<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; np.random.seed(12)\r\n&gt;&gt;&gt; maharashtra_ages1=stats.poisson.rvs(loc=18,mu=33,size=30)\r\n&gt;&gt;&gt; maharashtra_ages2=stats.poisson.rvs(loc=18,mu=13,size=20)\r\n&gt;&gt;&gt; maharashtra_ages=np.concatenate((maharashtra_ages1,maharashtra_ages2))\r\n&gt;&gt;&gt; maharashtra_ages.mean()<\/pre>\n<p><strong><span style=\"font-family: Verdana, Geneva, sans-serif\">42.26<\/span><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.ttest_ind(a=gujarat_ages,b=maharashtra_ages,equal_var=False)<\/pre>\n<p><strong><span style=\"font-family: Verdana, Geneva, sans-serif\">Ttest_indResult(statistic=-1.4415218453964938, pvalue=0.1526272389714945)<\/span><\/strong><br \/>\n<span style=\"font-weight: 400\">The value of 0.152 tells us there\u2019s a 15.2% chance that the sample data is so far apart for two identical groups. This is greater than the 5% confidence level.<\/span><\/p>\n<h4><strong>c. Paired T-test With Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">When you want to check how different samples from the same group are, you can go for a paired T-test. Let\u2019s take an example.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; np.random.seed(11)\r\n&gt;&gt;&gt; before=stats.norm.rvs(scale=30,loc=250,size=100)\r\n&gt;&gt;&gt; after=before+stats.norm.rvs(scale=5,loc=-1.25,size=100)\r\n&gt;&gt;&gt; weight_df=pd.DataFrame({\"weight_before\":before,\r\n                         \"weight_after\":after,\r\n                         \"weight_change\":after-before})\r\n&gt;&gt;&gt; weight_df.describe()<\/pre>\n<div id=\"attachment_23131\" style=\"width: 417px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/paired.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23131\" class=\"wp-image-23131 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/paired.png\" alt=\"Python Statistics\" width=\"407\" height=\"161\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/paired.png 407w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/paired-150x59.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/paired-300x119.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/a><p id=\"caption-attachment-23131\" class=\"wp-caption-text\">Paired Sample T-test<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.ttest_rel(a=before,b=after)<\/pre>\n<p><strong>Ttest_relResult(statistic=2.5720175998568284, pvalue=0.011596444318439857)<\/strong><br \/>\n<span style=\"font-weight: 400\">So, we see we have just 1% chances to find such huge differences between samples.<\/span><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/errors-and-exceptions-in-python\/\" target=\"_blank\" rel=\"noopener\">Do you know about Python Errors<\/a>?<\/strong><\/p>\n<h3><strong>KS Test in Python Statistics<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">This is the Kolmogorov-Smirnov<\/span> <span style=\"font-weight: 400\">test. It lets us test the hypothesis that the sample is a part of the standard t-distribution. Let\u2019s take an example.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.kstest(x,'t',(10,))<\/pre>\n<p><span style=\"font-weight: 400\"><strong>KstestResult(statistic=0.023682909426459897, pvalue=0.6289865281325614)<\/strong><\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.kstest(x,'norm')<\/pre>\n<p><strong>KstestResult(statistic=0.019334747291889, pvalue=0.8488119233062457)<\/strong><br \/>\n<span style=\"font-weight: 400\">Pay attention to the p-values in both cases.<\/span><\/p>\n<h4><strong>a. Two samples<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">What we saw above was the KS test for one sample. Let\u2019s try two.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; stats.ks_2samp(gujarat_ages,maharashtra_ages)<\/pre>\n<p><strong>Ks_2sampResult(statistic=0.26, pvalue=0.056045859714424606)<\/strong><\/p>\n<h3><strong>Correlation in Python Statistics<\/strong><\/h3>\n<p>Correlation measures the strength and direction of a relationship between two variables. The most common type is Pearson correlation, which ranges from -1 to +1. A value close to +1 indicates a strong positive relationship, while -1 indicates a strong negative relationship.<\/p>\n<p><strong>Limitations of correlation:<\/strong><\/p>\n<ul>\n<li>If two things happen at the same time, it doesn\u2019t mean that one of them caused it.<\/li>\n<li>It does not understand straight lines.<\/li>\n<\/ul>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/exception-handling-in-python\/\" target=\"_blank\" rel=\"noopener\">Have a look at Exception Handling in Python<\/a><\/strong><\/p>\n<p><span style=\"font-weight: 400\">Correlation can denote a predictive relationship that we can exploit. To measure the degree of correlation, we can use constants like \u03c1 or r. Benefits of correlation-<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Predicting one quantity from another<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Discovering the existence of a causal relationship<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Foundation for other modeling techniques<\/span><\/li>\n<\/ul>\n<h4><strong>a. Example of Correlation In Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Let\u2019s take an example.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; df=pd.read_csv('furniture.csv',index_col='Serial',parse_dates=True)\r\n&gt;&gt;&gt; df['Gross']=df.Cost+df.Cost*10\r\n&gt;&gt;&gt; df.describe()<\/pre>\n<div id=\"attachment_23133\" style=\"width: 278px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/describe-1-1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23133\" class=\"wp-image-23133 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/describe-1-1.png\" alt=\"Python Statistics\" width=\"268\" height=\"163\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/describe-1-1.png 268w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/describe-1-1-150x91.png 150w\" sizes=\"auto, (max-width: 268px) 100vw, 268px\" \/><\/a><p id=\"caption-attachment-23133\" class=\"wp-caption-text\">Python Statistics &#8211; Correlation With Python<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; df.corr()<\/pre>\n<div id=\"attachment_23134\" style=\"width: 166px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/corr.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23134\" class=\"wp-image-23134 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/corr.png\" alt=\"Python Statistics\" width=\"156\" height=\"61\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/corr.png 156w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/corr-150x59.png 150w\" sizes=\"auto, (max-width: 156px) 100vw, 156px\" \/><\/a><p id=\"caption-attachment-23134\" class=\"wp-caption-text\">Example of Python Correlation<\/p><\/div>\n<p><span style=\"font-weight: 400\">This gives us how each column correlates to another. You can also calculate the covariance in the following way-<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; df.cov()<\/pre>\n<p><strong> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Cost \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Gross<\/strong><br \/>\n<strong>Cost \u00a0\u00a0 3.131608e+08 \u00a0 3.444769e+09<\/strong><br \/>\n<strong>Gross \u00a0 3.444769e+09 \u00a0 3.789246e+10<\/strong><\/p>\n<h4><strong>b. Plotting Correlation in Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Let\u2019s use seaborn to plot the correlation between columns of the \u2018iris\u2019 dataset.<\/span><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-iterator\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s revise Python Iterator<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import seaborn as sn\r\n&gt;&gt;&gt; df1=sn.load_dataset('iris')\r\n&gt;&gt;&gt; sn.pairplot(df,kind='scatter')<\/pre>\n<p><strong>&lt;seaborn.axisgrid.PairGrid object at 0x06294090&gt;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.show()<\/pre>\n<div id=\"attachment_23136\" style=\"width: 1285px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23136\" class=\"wp-image-23136 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots.png\" alt=\"Python Statistics\" width=\"1275\" height=\"633\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots.png 1275w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots-300x149.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots-768x381.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/plots-1024x508.png 1024w\" sizes=\"auto, (max-width: 1275px) 100vw, 1275px\" \/><\/a><p id=\"caption-attachment-23136\" class=\"wp-caption-text\">Plotting Correlation in Python<\/p><\/div>\n<h4><strong>c. Saving the Results<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">We can export the result of a correlation as a CSV file.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d=df1.corr()\r\n&gt;&gt;&gt; d.to_csv('iriscorrelation.csv')<\/pre>\n<p><span style=\"font-weight: 400\">This gives us the following CSV file-<\/span><\/p>\n<div id=\"attachment_23137\" style=\"width: 382px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/save.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-23137\" class=\"wp-image-23137 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/save.png\" alt=\"Python Statistics\" width=\"372\" height=\"148\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/save.png 372w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/save-150x60.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/save-300x119.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><p id=\"caption-attachment-23137\" class=\"wp-caption-text\">Python Statistics &#8211; Saving the Results<\/p><\/div>\n<p>So, this was all in Python Statistics. Hope you like our explanation.<\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Hence, in this Python Statistics tutorial, we discussed the p-value, T-test, correlation, and KS test with Python. To conclude, we\u2019ll say that a p-value is a numerical measure that tells you whether the sample data falls consistently with the null hypothesis. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Correlation is the interdependence of variable quantities. It measures the strength and direction of a relationship between two variables. The most common type is Pearson correlation, which ranges from -1 to +1. A value close to +1 indicates a strong positive relationship, while -1 indicates a strong negative relationship.\u00a0<\/span><\/p>\n<p>Understanding both p-values and correlations helps you validate findings, avoid misleading patterns, and improve your models. For example, you might find a high correlation between two input features, which could hurt your model\u2019s accuracy due to redundancy.<\/p>\n<p>Or you might find a low p-value that supports your theory about a treatment\u2019s effect. These tools are foundational for any statistical analysis in data science.<\/p>\n<p><span style=\"font-weight: 400\">Still, if any doubt regarding Python Statistics, ask in the comments tab.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Statistics tutorial, we will learn how to calculate the p-value and Correlation in Python. Moreover, we will discuss the T-test and the KS Test with examples and code in Python Statistics.&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":23149,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[3003,3005,4260,8052,9216,9371,9391,9540,10751,10855,10859,13807,14130,14990],"class_list":["post-23123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-correlation-in-python-statistics","tag-correlation-with-python","tag-example-of-correlation-in-python","tag-ks-test","tag-one-sample-t-test","tag-p-value-in-python-statistics","tag-paired-t-test-python","tag-plotting-correlation-in-python","tag-python-p-value","tag-python-statistics","tag-python-statistics-tutorial","tag-statistics-in-python","tag-t-test-in-python-statistics","tag-two-sample-t-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Statistics - Python p-Value, Correlation, T-test, KS Test - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about Python p-value ,Python T-test, one sample and Two Sample T-test,Paired Sample T-test,correlation in Python, Python KS test\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-statistics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Statistics - Python p-Value, Correlation, T-test, KS Test - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about Python p-value ,Python T-test, one sample and Two Sample T-test,Paired Sample T-test,correlation in Python, Python KS test\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-statistics\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-29T04:00:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-29T11:46:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Statistics - Python p-Value, Correlation, T-test, KS Test - DataFlair","description":"Learn about Python p-value ,Python T-test, one sample and Two Sample T-test,Paired Sample T-test,correlation in Python, Python KS test","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-statistics\/","og_locale":"en_US","og_type":"article","og_title":"Python Statistics - Python p-Value, Correlation, T-test, KS Test - DataFlair","og_description":"Learn about Python p-value ,Python T-test, one sample and Two Sample T-test,Paired Sample T-test,correlation in Python, Python KS test","og_url":"https:\/\/data-flair.training\/blogs\/python-statistics\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-29T04:00:36+00:00","article_modified_time":"2026-04-29T11:46:08+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Statistics &#8211; Python p-Value, Correlation, T-test, KS Test","datePublished":"2018-07-29T04:00:36+00:00","dateModified":"2026-04-29T11:46:08+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/"},"wordCount":1095,"commentCount":7,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg","keywords":["Correlation in Python Statistics","correlation with Python","Example of Correlation In Python","KS Test","one sample t-test","p-value in Python Statistics","paired t-test Python","Plotting Correlation in Python","Python P value","Python Statistics","Python Statistics tutorial","Statistics in Python","T-test in Python Statistics","two sample t-test"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-statistics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/","url":"https:\/\/data-flair.training\/blogs\/python-statistics\/","name":"Python Statistics - Python p-Value, Correlation, T-test, KS Test - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg","datePublished":"2018-07-29T04:00:36+00:00","dateModified":"2026-04-29T11:46:08+00:00","description":"Learn about Python p-value ,Python T-test, one sample and Two Sample T-test,Paired Sample T-test,correlation in Python, Python KS test","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-statistics\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Statistics-01.jpg","width":1200,"height":628,"caption":"Python Statistics - p-Value, Correlation, T-test, KS Test"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-statistics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Statistics &#8211; Python p-Value, Correlation, T-test, KS Test"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=23123"}],"version-history":[{"count":11,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23123\/revisions"}],"predecessor-version":[{"id":148134,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/23123\/revisions\/148134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/23149"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=23123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=23123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=23123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}