I have this function
JavaScript
x
9
1
def highest_correlation(dataframe):
2
corr_table = dataframe.corr().unstack()
3
df_corrvalues = corr_table.sort_values(ascending=False)
4
5
return df_corrvalues
6
7
correlation = highest_correlation(heart)
8
correlation
9
This is the output
JavaScript
1
14
14
1
age age 1.000000
2
sex sex 1.000000
3
thall thall 1.000000
4
caa caa 1.000000
5
slp slp 1.000000
6
7
output oldpeak -0.429146
8
exng -0.435601
9
exng output -0.435601
10
slp oldpeak -0.576314
11
oldpeak slp -0.576314
12
Length: 196, dtype: float64
13
14
How can return the highest correlation values that are lower than 1?
That is, I want to remove the 1s that appear on the top when I use sort_values(ascending=False)
Advertisement
Answer
Multiindex Series from the Pandas User Guide
JavaScript
1
15
15
1
import pandas as pd
2
from numpy.random import default_rng
3
rng = default_rng()
4
5
6
arrays = [
7
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
8
["one", "two", "one", "two", "one", "two", "one", "two"],
9
]
10
11
tuples = list(zip(*arrays))
12
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
13
s = pd.Series(rng.standard_normal(8), index=index)
14
print(s)
15
Filter for values less than one.
JavaScript
1
2
1
print(s[s<1])
2
JavaScript
1
19
19
1
first second
2
bar one 1.602675
3
two -0.197277
4
baz one -0.746729
5
two 1.384208
6
foo one 1.587294
7
two -1.616769
8
qux one -0.872030
9
two -0.721226
10
dtype: float64
11
12
first second
13
bar two -0.197277
14
baz one -0.746729
15
foo two -1.616769
16
qux one -0.872030
17
two -0.721226
18
dtype: float64
19