Correlation helps us see how two things change together. It tells if one thing goes up when the other goes up or down.
Correlation with corr() in Data Analysis Python
DataFrame.corr(method='pearson')This method calculates correlation between numeric columns in a DataFrame.
The default method is 'pearson', which measures linear correlation.
df.df.corr()
df.corr(method='pearson')df.corr(method='kendall')df.corr(method='spearman')This code creates a small dataset with study hours, test scores, and sleep hours. Then it calculates the correlation matrix to see how these columns relate.
import pandas as pd # Create a simple dataset data = { 'study_hours': [2, 3, 5, 8, 10], 'test_score': [50, 55, 65, 80, 90], 'sleep_hours': [7, 6, 5, 6, 7] } df = pd.DataFrame(data) # Calculate correlation matrix corr_matrix = df.corr() print(corr_matrix)
Correlation values range from -1 to 1.
A value close to 1 means strong positive relation, close to -1 means strong negative relation, and around 0 means no relation.
Only numeric columns are used in correlation calculation.
Use corr() to find relationships between numeric columns.
Correlation helps understand how variables move together.
Values near 1 or -1 show strong relationships; near 0 means weak or no relationship.
Practice
corr() function in pandas do?Solution
Step 1: Understand the purpose of
Thecorr()corr()function calculates how numeric columns relate to each other in a DataFrame.Step 2: Compare with other options
Counting missing values, sorting, or filtering are different functions likeisna(),sort_values(), andloc.Final Answer:
Calculates the correlation between numeric columns in a DataFrame -> Option BQuick Check:
corr() finds relationships [OK]
- Confusing corr() with counting or sorting functions
- Thinking corr() works on non-numeric data
- Assuming corr() changes the DataFrame structure
df?Solution
Step 1: Recall the exact method name
The correct pandas method to calculate correlation iscorr().Step 2: Check each option
Options A, B, and C are misspelled or incorrect method names.Final Answer:
df.corr() -> Option AQuick Check:
Use df.corr() for correlation [OK]
- Misspelling the method name
- Using non-existent pandas methods
- Confusing corr() with other functions
df:
A B C 0 1 2 5 1 2 4 7 2 3 6 9 3 4 8 11What is the output of
df.corr()?Solution
Step 1: Understand the data pattern
Columns A, B, and C increase perfectly linearly: B = 2*A, C = 2*A + 3.Step 2: Calculate correlation values
Since all columns are perfectly linearly related, correlation between any two is 1.0, and correlation with itself is 1.0.Final Answer:
{'A': {'A': 1.0, 'B': 1.0, 'C': 1.0}, 'B': {'A': 1.0, 'B': 1.0, 'C': 1.0}, 'C': {'A': 1.0, 'B': 1.0, 'C': 1.0}} -> Option AQuick Check:
Perfect linear relation means correlation = 1 [OK]
- Assuming correlation less than 1 for perfect linear data
- Confusing correlation with covariance
- Thinking corr() raises error on numeric data
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3], 'Y': ['a', 'b', 'c']})
print(df.corr(numeric_only=False))
What is the cause of the error?Solution
Step 1: Analyze DataFrame columns
Column 'Y' contains strings, which are non-numeric.Step 2: Understand corr() requirements
corr() only works on numeric columns; non-numeric columns cause it to ignore or error depending on pandas version.Final Answer:
The DataFrame has non-numeric columns which corr() cannot process -> Option CQuick Check:
corr() needs numeric data [OK]
- Assuming corr() works on strings
- Thinking misspelling caused error
- Ignoring data types in DataFrame
df with columns height, weight, and age. You want to find which two variables have the strongest positive correlation. Which code snippet correctly finds this?Solution
Step 1: Calculate correlation matrix
Usedf.corr()to get correlations between all numeric columns.Step 2: Exclude self-correlation (which is 1)
Usewhere(corr_matrix != 1)to ignore diagonal 1s, then find the maximum correlation value.Step 3: Find the strongest positive correlation
Usemax().max()to get the highest correlation excluding 1.Final Answer:
corr_matrix = df.corr() max_corr = corr_matrix.where(corr_matrix != 1).max().max() -> Option DQuick Check:
Exclude 1, then find max correlation [OK]
- Including diagonal 1s in max correlation
- Using min() instead of max()
- Not excluding self-correlation values
Ready to prove it?
Pass the test and this pattern moves unknown
