Reputation: 219
I have a JSON data like:
a={('A', 'B'): 0.8333333333333334, ('A', 'C'): 0.5, ('B', 'C'): 0.625}
I need to draw a heatmap based on that data set. but the dataframe I obtained by
df=pd.DataFrame(a,index=['value']).T.reset_index()
is like:
and this seems can not be used to draw a heat map. And the result dataframe should be looked like this:
df=pd.DataFrame({'A':{'A':1,'B':0.83333,'C':0.5},'B':{'A':0.83333,'B':1,'C':0.625},'C':{'A':0.5,'B':0.625,'C':1}})
Upvotes: 1
Views: 146
Reputation: 260420
Assuming:
a = {('A', 'B'): 0.8333333333333334, ('A', 'C'): 0.5, ('B', 'C'): 0.625}
you can convert to Series and unstack
to get a DataFrame:
df = pd.Series(a).unstack()
output:
B C
A 0.833333 0.500
B NaN 0.625
but this gives only single combinations. If you want both ways, combine_first
with the transpose
(T):
df = df.combine_first(df.T).fillna(1)
output:
A B C
A 1.000000 0.833333 0.500
B 0.833333 1.000000 0.625
C 0.500000 0.625000 1.000
For your heatmap, you can use seaborn.heatmap
:
import seaborn as sns
sns.heatmap(df)
output:
Upvotes: 3