如何从词典列表中创建FusionCharts热图?

问题描述

我想在python中使用FusionCharts热图显示以下数据:

[{'day_of_like': 'Monday','hours_of_like': 18,'avg_of_likes': 8},{'day_of_like': 'Monday','hours_of_like': 23,'avg_of_likes': 5}]

但是我在FusionCharts网站上找不到该指南。我该如何使用python做到这一点?

解决方法

import pandas as pd
import seaborn as sns

# list of dictionaries
data = [{'day_of_like': 'Monday','hours_of_like': 18,'avg_of_likes': 8},{'day_of_like': 'Monday','hours_of_like': 23,'avg_of_likes': 5}]

# convert to dataframe
df = pd.json_normalize(data)

# save the data to a csv if needed
df.to_csv('test.csv',index=False)

# display(df)
  day_of_like  hours_of_like  avg_of_likes
0      Monday             18             8
1      Monday             23             5

# to json
df.to_json()

[out]:
'{"day_of_like":{"0":"Monday","1":"Monday"},"hours_of_like":{"0":18,"1":23},"avg_of_likes":{"0":8,"1":5}}'

# to dict
df.to_dict()

[out]:
{'day_of_like': {0: 'Monday',1: 'Monday'},'hours_of_like': {0: 18,1: 23},'avg_of_likes': {0: 8,1: 5}}

# plot
sns.heatmap(df[['hours_of_like','avg_of_likes']])

enter image description here

,

根据共享的数据,您似乎必须复制day_of_like属性,您需要为每个重复的键设置一个唯一值或设置一个唯一值,这里是FusionCharts中JavaScript的相同表示形式-{{3} }

  "dataset": [{
          "data": [{
              "rowid": "hl","columnid": "1","value": "18","colorRangeLabel": "Bad"
            },{
              "rowid": "al","value": "8",{
              "rowid": "hl","columnid": "2","value": "23","colorRangeLabel": "Good"
            },"value": "5","colorRangeLabel": "Bad"
            }
          ]
        }]