# How to perform bivariate analysis in Python?

By Shivang Yadav Last updated : November 22, 2023

## Bivariate Analysis

Bivariate analysis is a statistical method that explores the relationship between two variables. Unlike univariate analysis, which focuses on a single variable, bivariate analysis examines the association between two variables. The primary goal of bivariate analysis is to understand whether changes in one variable are associated with changes in another variable and to quantify the strength and direction of that association.

## Bivariate Analysis - Key Aspects

Key aspects of bivariate analysis include -

1. Types of Variables: The two variables being studied can be of different types, such as:
• Categorical-Categorical: Analyzing the relationship between two categorical variables.
• Categorical-Numerical: Studying how a numerical variable differs across different categories.
• Numerical-Numerical: Examining the correlation or association between two numerical variables.
2. Visualization: The analysis can be done using graphs plotting their types.
• Scatter plots: Visualization of relationship between two numerical variables.
• Bar charts: Comparing the distribution of a numerical variable across different categories.
• Cross-tabulation tables: Summarize the joint frequencies of two categorical variables.
3. Measures of Association:
• Categorical-categorical relationship: Use of measures like chi-squared tests or measures of association like Cramer's V.
• Numerical-numerical relationship: Correlation coefficients like Pearson's correlation coefficient or Spearman's rank correlation coefficient can quantify the strength and direction of the association.
4. Hypothesis Testing: Bivariate analysis often involves testing hypotheses to determine whether the observed relationship is statistically significant or if it could have occurred by chance.
5. Causality vs. Correlation: It's essential to recognize that a significant association between two variables does not imply causation.
6. Confounding Variables: Bivariate analysis may uncover associations, but other variables (confounding variables) may influence the relationship.
7. Interpretation: Interpret the results to draw insights and make informed decisions. Consider the practical implications of the relationship between the two variables.

Bivariate analysis is a fundamental step in the data analysis process, as it helps researchers and analysts explore the relationships between variables and identify patterns and trends. It provides a foundation for more advanced statistical techniques, such as multivariate analysis, which involves studying the relationships between three or more variables simultaneously.

## Performing Bivariate Analysis

To perform bivariate analysis, get the correlation coefficient using the DataFrame.corr() method which calculates the pairwise correlation of columns, excluding NA/null values.

## Example

Python program to perform bivariate analysis using correlation coefficient.

```import pandas as pd

values = pd.DataFrame(
{
"hours": [1, 1, 2, 2, 3, 3, 3, 3, 5, 5, 6, 6, 7, 8],
"score": [75, 66, 78, 72, 85, 90, 82, 80, 90, 92, 94, 94, 91, 96],
}
)

print(f"The dataset is \n{values}")

corrCoef = values.corr()
print(f"The correlation Coefficient is \n{corrCoef}")
```

### Output

The output of the above example is:

```The dataset is
hours  score
0       1     75
1       1     66
2       2     78
3       2     72
4       3     85
5       3     90
6       3     82
7       3     80
8       5     90
9       5     92
10      6     94
11      6     94
12      7     91
13      8     96
The correlation Coefficient is
hours     score
hours  1.000000  0.885031
score  0.885031  1.000000```