3 examples of 'pandas correlation between two columns' in Python

Every line of 'pandas correlation between two columns' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
143def correlation(T, a, b, lagtimes):
144 """
145 computes the time correlation function of a and b
146 """
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

7def test_corr_pandas():
8 df = pd.DataFrame(
9 {
10 "x1": [1, 2, 3, 4, 5, 6, 7, 8],
11 "x2": [0, 0, 0, 1, 0, 0, 0, 0],
12 "y": [2, 3, 4, 6, 6, 7, 8, 9],
13 }
14 )
15
16 mod = Ridge().fit(df[["x1", "x2"]], df["y"])
17 assert abs(correlation_score("x1")(mod, df[["x1", "x2"]])) > abs(0.99)
18 assert abs(correlation_score("x2")(mod, df[["x1", "x2"]])) < abs(0.02)
130def correlation(x, y):
131 stdev_x = standard_deviation(x)
132 stdev_y = standard_deviation(y)
133 if stdev_x > 0 and stdev_y > 0:
134 return covariance(x, y) / stdev_x / stdev_y
135 else:
136 return 0 # if no variation, correlation is zero

Related snippets