Pandas Correlations help you understand the relationship between numerical columns in a DataFrame. Using the corr() method, you can check whether two values increase together, move in opposite directions, or have no clear linear relationship. This is useful when analyzing business, sales, student, and other datasets.
1. What Is Correlation in Pandas?
Correlation measures how strongly two numerical columns are related.
For example, you may want to check whether increasing study hours is related to getting higher marks.
Correlation values range from -1 to +1.
| Correlation Value | Meaning |
|---|---|
| +1 | Perfect positive correlation |
| Close to +1 | Strong positive correlation |
| 0 | No linear correlation |
| Close to -1 | Strong negative correlation |
| -1 | Perfect negative correlation |
2. Positive Correlation
Positive correlation means that when one value increases, the other value also tends to increase.
For example:
- More study hours may be associated with higher marks.
- More workout duration may be associated with more calories burned.
- Higher advertising spending may be associated with increased sales.
3. Negative Correlation
Negative correlation means that when one value increases, the other value tends to decrease.
For example:
- As product prices increase, demand may decrease.
- As travel speed increases, travel time for the same distance decreases.
- As product discounts increase, profit margins may decrease.
4. No Correlation
No linear correlation means there is no clear straight-line relationship between two numerical columns.
For example, a person’s shoe size and their examination marks may have no meaningful relationship.
5. Using the corr() Method
Pandas provides the corr() method to calculate correlations between numerical columns.
Syntax:
DataFrame.corr()
Example:
import pandas as pd
data = {
"Study_Hours": [1, 2, 3, 4, 5],
"Marks": [40, 50, 60, 70, 80]
}
df = pd.DataFrame(data)
print(df.corr())
Output:
Study_Hours Marks
Study_Hours 1.0 1.0
Marks 1.0 1.0
Explanation:
df.corr()calculates the correlation between numerical columns.- The correlation between
Study_HoursandMarksis1.0. - This means both columns have a perfect positive linear relationship in this example.
- The diagonal values are always
1.0because each column is perfectly correlated with itself.
6. Correlation Between Multiple Columns
You can use corr() to calculate correlations between several numerical columns at once.
Example:
import pandas as pd
data = {
"Study_Hours": [1, 2, 3, 4, 5],
"Marks": [40, 50, 60, 70, 80],
"Sleep_Hours": [8, 7, 6, 5, 4]
}
df = pd.DataFrame(data)
print(df.corr())
Output:
Study_Hours Marks Sleep_Hours
Study_Hours 1.0 1.0 -1.0
Marks 1.0 1.0 -1.0
Sleep_Hours -1.0 -1.0 1.0
Explanation:
Study_HoursandMarkshave a positive correlation of1.0.Study_HoursandSleep_Hourshave a negative correlation of-1.0.MarksandSleep_Hoursalso have a negative correlation of-1.0.
This result is called a correlation matrix. It displays the correlation between every pair of numerical columns.
7. Correlation Methods in Pandas
The corr() method supports three correlation calculation methods.
| Method | Description |
|---|---|
pearson | Measures linear relationships between numerical columns. This is the default method. |
kendall | Measures relationships using the ranking of values. |
spearman | Measures relationships using ranked values and identifies monotonic relationships. |
Example:
import pandas as pd
data = {
"Sales": [100, 200, 300, 400, 500],
"Profit": [20, 40, 60, 80, 100]
}
df = pd.DataFrame(data)
print(df.corr(method="pearson"))
print(df.corr(method="kendall"))
print(df.corr(method="spearman"))
Explanation:
method="pearson"calculates Pearson correlation.method="kendall"calculates Kendall rank correlation.method="spearman"calculates Spearman rank correlation.
All three methods return 1.0 for these perfectly increasing values.
8. Important Points About Pandas Correlations
- Correlation is calculated between suitable numerical columns.
corr()returns a correlation matrix.- Correlation values range from
-1to+1. - A positive value indicates a positive relationship.
- A negative value indicates a negative relationship.
- A value near zero indicates little or no linear relationship.
- Correlation does not prove that one variable causes another.
Remember: Two columns can be correlated without one causing changes in the other.
Student Task
Create a Pandas DataFrame using the following student data:
| Student | Study_Hours | Marks | Sleep_Hours |
|---|---|---|---|
| A | 1 | 35 | 8 |
| B | 2 | 45 | 7 |
| C | 3 | 55 | 6 |
| D | 4 | 65 | 5 |
| E | 5 | 75 | 4 |
Complete these tasks:
- Create a DataFrame using the given data.
- Display the DataFrame.
- Calculate the correlation matrix using
corr(). - Find the correlation between
Study_HoursandMarks. - Find the correlation between
Study_HoursandSleep_Hours. - Calculate the correlation matrix using the
spearmanmethod.
Key Takeaways
- Correlation measures the relationship between numerical columns.
- Pandas uses the
corr()method to calculate correlation. - Correlation values range from
-1to+1. - Positive correlation means values tend to increase together.
- Negative correlation means one value tends to decrease as the other increases.
- Pearson, Kendall, and Spearman are supported correlation methods.
- Correlation does not establish cause and effect.
Frequently Asked Questions (FAQs)
1. What is correlation in Pandas?
Correlation in Pandas measures the relationship between numerical columns in a DataFrame. It helps identify positive, negative, or weak linear relationships.
2. Which method is used to calculate correlation in Pandas?
The corr() method is used to calculate correlations between numerical columns.
3. What is the range of correlation values?
Correlation values range from -1 to +1. A value of +1 indicates perfect positive correlation, while -1 indicates perfect negative correlation.
4. What does a positive correlation mean?
Positive correlation means that two variables tend to increase together. For example, study hours and marks may have a positive relationship.
5. What does a negative correlation mean?
Negative correlation means that one variable tends to decrease when the other increases.
6. What are the different correlation methods in Pandas?
Pandas supports three correlation methods: Pearson, Kendall, and Spearman.
7. Does Pandas calculate correlation for text columns?
The corr() method calculates correlations between suitable numerical columns. Text columns are not used in the calculation.
8. Does correlation prove that one variable causes another?
No. Correlation shows a relationship between variables, but it does not prove that one variable causes changes in another.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
