Issue
I have a dataframe as such
col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
2 12 8 3 16 9 5 13 11 14
3 14 7 9 18 12 14 14 13 13
5 15 10 5 8 10 18 9 8 5
I want the output to produce a heatmap where col1-col5 are compared vs col6-col10
col1
col2
col3
col4
col5
col6 col7 col8 col9 col10
How can I achieve such a structure?
Solution
The following example code shows how you can correlate each of the last 5 columns with each of the first 5 columns, and show the result as a heatmap.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
# create some test data
df = pd.DataFrame(np.random.rand(3, 10), columns=[f'col{i}' for i in range(1, 11)])
# find the correlations between the last 5 columns with each of the 5 first columns
corr = df[df.columns[5:]].apply(lambda x: df[df.columns[:5]].corrwith(x))
# show the correlations as a heatmap
ax = sns.heatmap(corr, annot=True, cmap='RdYlBu', vmin=-1, vmax=1)
ax.tick_params(labelrotation=0)
plt.tight_layout()
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.