liveBook · Manning

Correlation

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A statistical measure of the strength and direction of association between the number of wines submitted by a country and its mean score.

Correlation describes whether two measurements tend to move together. When both generally rise or fall together, the relationship is positive; when one tends to rise as the other falls, it is negative. A relationship can also be weak or strong. Correlation is an association measure, not proof that one measurement causes the other. section 11.12

In pandas, calculate numeric relationships with `corr()`. Calling it on a DataFrame returns a matrix: each column is compared with every other column, and each variable has a correlation of 1 with itself. A value of 0 indicates no correlation, 1 perfect alignment, and –1 complete opposition. section 11.12 section 13.3

high       low
high  1.000000  0.105603
low   0.105603  1.000000

A `corr()` result is symmetric: the high/low comparison appears in both off-diagonal cells, while the diagonal records self-correlation. section 11.12

Prepare comparable data before correlating

Correlation is most useful after the relevant numeric columns share the same observations. For oil price, ice-cream price, and miles driven, parse dates, use dates as indexes, join the three DataFrames, and retain only dates common to all of them. Then call `df.corr()` and inspect scatter plots for the oil–ice-cream and oil–miles relationships. Cleaning nonnumeric values and handling missing data are part of the analysis, not an afterthought. section 11.13 section 11.14

Use correlation as a compact follow-up to a concrete question:

  • Compare a country’s mean wine score with its mean tourism spending after grouping and joining country-level results; inspect `NaN` values because join type changes which countries remain. section 7.13
  • Compare the number of wines submitted by each country with that country’s mean score, after aggregating reviews by country. section 9.8
  • Compare admission rate and tuition with `institutions_df[['ADM_RATE', 'TUITIONFEE_OUT']].corr()`, then use a scatter plot to check the overall pattern. section 13.3

A scatter plot complements the number. The tuition and admission-rate plot trends from top left toward bottom right, matching a reported correlation of –0.3: tuition tends to increase as admission rate tends to decrease, but only slightly. Plotting helps reveal the shape of an association rather than treating one value as a complete explanation. section 13.3

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Defines direction and strength, interprets values from –1 to 1, and applies `corr()` alongside scatter plots.
Examples
Uses temperatures, oil prices, ice-cream prices, miles driven, wine scores, tourism spending, tuition, and admission rates.
What it assumes
Assumes ability to load, select, clean, index, join, and group pandas DataFrames.
What you can do after
Apply it via pandas `corr()` to quantify numeric associations and inspect them with scatter plots.
How it connects
Connects correlation with pandas DataFrames, joins, grouping, missing values, and scatter plots.

Also explained in

Compare how 16 books explain correlation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.