排序并选择前5个JSON值
我有一个双重问题,我在寻找如何解决这个问题的线索 我有一个json文件,其格式如下:排序并选择前5个JSON值,json,python-3.x,Json,Python 3.x,我有一个双重问题,我在寻找如何解决这个问题的线索 我有一个json文件,其格式如下: { "code": 2000, "data": { "1": { "attribute1": 40, "attribute2": 1.4, "attribute3": 5.2,
{
"code": 2000,
"data": {
"1": {
"attribute1": 40,
"attribute2": 1.4,
"attribute3": 5.2,
"attribute4": 124
"attribute5": "65.53%"
},
"94": {
"attribute1": 10,
"attribute2": 4.4,
"attribute3": 2.2,
"attribute4": 12
"attribute5": "45.53%"
},
"96": {
"attribute1": 17,
"attribute2": 9.64,
"attribute3": 5.2,
"attribute4": 62
"attribute5": "51.53%"
}
},
"message": "SUCCESS"
}
我的目标是:
首先,我想按任何属性对数据进行排序。
大约有100个,我想根据它们的分类方式来抓取前5个,然后。。。
在表格中输出数据,例如:
*此外,上面的attribute5是一个字符串值
诚然,我在这里的知识非常有限。
我试图模仿这里使用的方法:
我设法打开了文件,可以从示例行中提取键值:
import json
jsonfile = path-to-my-file.json
with open(jsonfile) as j:
data=json.load(j)
k = data["data"]["1"].keys()
print(k)
total=data["data"]
for row in total:
v = data["data"][str(row)].values()
print(v)
这将产生:
dict_keys(['attribute1', 'attribute2', 'attribute3', 'attribute4', 'attribute5'])
dict_values([1, 40, 1.4, 5.2, 124, '65.53%'])
dict_values([94, 10, 4.4, 2.2, 12, '45.53%'])
dict_values([96, 17, 9.64, 5.2, 62, '51.53%'])
Sorted by: attribute5
Column attribute1 attribute2 attribute3 attribute4 attribute5
-------- ------------ ------------ ------------ ------------ ------------
1 40 1.4 5.2 124 65.53%
96 17 9.64 5.2 62 51.53%
94 10 4.4 2.2 12 45.53%
如果方向正确,我们将不胜感激
谢谢 如果你不介意使用熊猫,你可以这样做
import pandas as pd
rows = [v for k,v in data["data"].items()]
df = pd.DataFrame(rows)
# then to get the top 5 values by attribute can choose either ascending
# or descending with the ascending keyword and head prints the top 5 rows
df.sort_values('attribute1', ascending=True).head()
这将允许您随时按所需的任何属性进行排序,并打印出一个表
这将根据您的排序方式生成这样的输出
attribute1 attribute2 attribute3 attribute4 attribute5
0 40 1.40 5.2 124 65.53%
1 10 4.40 2.2 12 45.53%
2 17 9.64 5.2 62 51.53%
如果你不介意使用熊猫,你可以这样做
import pandas as pd
rows = [v for k,v in data["data"].items()]
df = pd.DataFrame(rows)
# then to get the top 5 values by attribute can choose either ascending
# or descending with the ascending keyword and head prints the top 5 rows
df.sort_values('attribute1', ascending=True).head()
这将允许您随时按所需的任何属性进行排序,并打印出一个表
这将根据您的排序方式生成这样的输出
attribute1 attribute2 attribute3 attribute4 attribute5
0 40 1.40 5.2 124 65.53%
1 10 4.40 2.2 12 45.53%
2 17 9.64 5.2 62 51.53%
如果你不想使用熊猫,我会把这个答案留在这里,但我建议你的答案不那么复杂 对于按特定属性排序,这应该可以:
import json
SORT_BY = "attribute4"
with open("test.json") as j:
data = json.load(j)
items = data["data"]
sorted_keys = list(sorted(items, key=lambda key: items[key][SORT_BY], reverse=True))
现在,sorted_keys是一个按属性排序的键列表
然后,为了将其打印为表格,我使用了库。我的最终代码如下所示:
from tabulate import tabulate
import json
SORT_BY = "attribute4"
with open("test.json") as j:
data = json.load(j)
items = data["data"]
sorted_keys = list(sorted(items, key=lambda key: items[key][SORT_BY], reverse=True))
print(f"\nSorted by: {SORT_BY}")
print(
tabulate(
[
[sorted_keys[i], *items[sorted_keys[i]].values()]
for i, _ in enumerate(items)
],
headers=["Column", *items["1"].keys()],
)
)
按“attribute5”排序时,这将输出:
dict_keys(['attribute1', 'attribute2', 'attribute3', 'attribute4', 'attribute5'])
dict_values([1, 40, 1.4, 5.2, 124, '65.53%'])
dict_values([94, 10, 4.4, 2.2, 12, '45.53%'])
dict_values([96, 17, 9.64, 5.2, 62, '51.53%'])
Sorted by: attribute5
Column attribute1 attribute2 attribute3 attribute4 attribute5
-------- ------------ ------------ ------------ ------------ ------------
1 40 1.4 5.2 124 65.53%
96 17 9.64 5.2 62 51.53%
94 10 4.4 2.2 12 45.53%
如果你不想使用熊猫,我会把这个答案留在这里,但我建议你的答案不那么复杂 对于按特定属性排序,这应该可以:
import json
SORT_BY = "attribute4"
with open("test.json") as j:
data = json.load(j)
items = data["data"]
sorted_keys = list(sorted(items, key=lambda key: items[key][SORT_BY], reverse=True))
现在,sorted_keys是一个按属性排序的键列表
然后,为了将其打印为表格,我使用了库。我的最终代码如下所示:
from tabulate import tabulate
import json
SORT_BY = "attribute4"
with open("test.json") as j:
data = json.load(j)
items = data["data"]
sorted_keys = list(sorted(items, key=lambda key: items[key][SORT_BY], reverse=True))
print(f"\nSorted by: {SORT_BY}")
print(
tabulate(
[
[sorted_keys[i], *items[sorted_keys[i]].values()]
for i, _ in enumerate(items)
],
headers=["Column", *items["1"].keys()],
)
)
按“attribute5”排序时,这将输出:
dict_keys(['attribute1', 'attribute2', 'attribute3', 'attribute4', 'attribute5'])
dict_values([1, 40, 1.4, 5.2, 124, '65.53%'])
dict_values([94, 10, 4.4, 2.2, 12, '45.53%'])
dict_values([96, 17, 9.64, 5.2, 62, '51.53%'])
Sorted by: attribute5
Column attribute1 attribute2 attribute3 attribute4 attribute5
-------- ------------ ------------ ------------ ------------ ------------
1 40 1.4 5.2 124 65.53%
96 17 9.64 5.2 62 51.53%
94 10 4.4 2.2 12 45.53%
这也很有效。这也很有效。这符合所有的目标,而且非常简单。我不熟悉熊猫图书馆。如果我要定期处理JSON数据,熊猫是最可持续/最直接的方法吗?@p3hndrx它通常是用python中的各种数据结构创建表格数据和更多数据的最佳方法。如果你打算在获得数据后对数据进行任何类型的数据分析,那么我肯定会建议使用熊猫,因为熊猫是为满足所有目标而设计的,而且非常简单。我不熟悉熊猫图书馆。如果我要定期处理JSON数据,熊猫是最可持续/最直接的方法吗?@p3hndrx它通常是用python中的各种数据结构创建表格数据和更多数据的最佳方法。如果你打算在获得数据后对其进行任何类型的数据分析,那么我肯定会建议使用熊猫,因为这就是它的设计目的