简体   繁体   English

如何正确地将我的 json 导出到 pandas 中的 dataframe

[英]How can i export correctly my json to a dataframe in pandas

can someone help me please?有人能帮助我吗? i'm kinda new with python and pandas我对 python 和 pandas 有点陌生

I'm trying to open a json into a pandas dataframe i'm using我正在尝试将 json 打开到 pandas dataframe 我正在使用

df3 = pd.read_json('/content/Top100.json', orient='records')
df3

But i'm getting my import incorrectly, it's showing the whole data from a record as a single cell like this:但是我的导入不正确,它将记录中的整个数据显示为单个单元格,如下所示:

0 0
{ "danceability": 0.615, "energy": 0.534, "key": 9, "loudness": -6.719, "mode": 0, "speechiness": 0.0386, "acousticness": 0.106, "instrumentalness": 0.0000176, "liveness": 0.0607, "valence": 0.193, "tempo": 135.917, "type": "audio_features", "id": "1R0a2iXumgCiFb7HEZ7gUE", "uri": "spotify:track:1R0a2iXumgCiFb7HEZ7gUE", "track_href": "https://api.spotify.com/v1/tracks/1R0a2iXumgCiFb7HEZ7gUE", "analysis_url": "https://api.spotify.com/v1/audio-analysis/1R0a2iXumgCiFb7HEZ7gUE", "duration_ms": 236413, "time_signature": 4} {“舞蹈能力”:0.615,“能量”:0.534,“键”:9,“响度”:-6.719,“模式”:0,“演讲”:0.0386,“声学”:0.106,“乐器”:0.0000176, “liveness”:0.0607,“valence”:0.193,“tempo”:135.917,“type”:“audio_features”,“id”:“1R0a2iXumgCiFb7HEZ7gUE”,“uri”:“spotify:track:1R0a2iXumgCiFb7HEZ7gUE”,“track_href”: “https://api.spotify.com/v1/tracks/1R0a2iXumgCiFb7HEZ7gUE”、“analysis_url”:“https://api.spotify.com/v1/audio-analysis/1R0a2iXumgCiFb7HEZ7gUE”、“duration_ms”:236413、“time_signature” “:4}
{ "danceability": 0.532, "energy": 0.623, "key": 5, "loudness": -9.208, "mode": 1, "speechiness": 0.0331, "acousticness": 0.538, "instrumentalness": 0.0000728, "liveness": 0.0925, "valence": 0.403, "tempo": 89.937, "type": "audio_features", "id": "3hUxzQpSfdDqwM3ZTFQY0K", "uri": "spotify:track:3hUxzQpSfdDqwM3ZTFQY0K", "track_href": "https://api.spotify.com/v1/tracks/3hUxzQpSfdDqwM3ZTFQY0K", "analysis_url": "https://api.spotify.com/v1/audio-analysis/3hUxzQpSfdDqwM3ZTFQY0K", "duration_ms": 261923, "time_signature": 4} {“舞蹈能力”:0.532,“能量”:0.623,“键”:5,“响度”:-9.208,“模式”:1,“演讲”:0.0331,“声学”:0.538,“乐器”:0.0000728, “liveness”:0.0925,“valence”:0.403,“tempo”:89.937,“type”:“audio_features”,“id”:“3hUxzQpSfdDqwM3ZTFQY0K”,“uri”:“spotify:track:3hUxzQpSfdDqwM3ZTFQY0K”,“track_href”: “https://api.spotify.com/v1/tracks/3hUxzQpSfdDqwM3ZTFQY0K”、“analysis_url”:“https://api.spotify.com/v1/audio-analysis/3hUxzQpSfdDqwM3ZTFQY0K”、“duration_ms”:261923、“time_signature “:4}

instead of代替

danceability跳舞能力 energy活力 key钥匙
0.615 0.615 0.534 0.534 9 9

Here's a piece of my json这是我的一块 json

[{"0":"{\n "danceability": 0.615,\n "energy": 0.534,\n "key": 9,\n "loudness": -6.719,\n "mode": 0,\n "speechiness": 0.0386,\n "acousticness": 0.106,\n "instrumentalness": 0.0000176,\n "liveness": 0.0607,\n "valence": 0.193,\n "tempo": 135.917,\n "type": "audio_features",\n "id": "1R0a2iXumgCiFb7HEZ7gUE",\n "uri": "spotify:track:1R0a2iXumgCiFb7HEZ7gUE",\n "track_href": "https://api.spotify.com/v1/tracks/1R0a2iXumgCiFb7HEZ7gUE",\n "analysis_url": "https://api.spotify.com/v1/audio-analysis/1R0a2iXumgCiFb7HEZ7gUE",\n "duration_ms": 236413,\n "time_signature": 4\n}"},{"0":"{\n "danceability": 0.532,\n "energy": 0.623,\n "key": 5,\n "loudness": -9.208,\n "mode": 1,\n "speechiness": 0.0331,\n "acousticness": 0.538,\n "instrumentalness": 0.0000728,\n "liveness": 0.0925,\n "valence": 0.403,\n "tempo": 89.937,\n "type": "audio_features",\n "id": "3hUxzQpSfdDqwM3ZTFQY0K",\n "uri": "spotify:track:3hUxzQpSfdDqwM3ZTFQY0K",\n "track_href": "https://api.spotify.com/v1/tracks/3hUxzQpSfdDqwM3ZTFQY0K",\ [{“0”:“{\n“舞蹈能力”:0.615,\n“能量”:0.534,\n“键”:9,\n“响度”:-6.719,\n“模式”:0,\ n “语音”:0.0386,\n “声学”:0.106,\n “器乐”:0.0000176,\n “活跃度”:0.0607,\n “效价”:0.193,\n “节奏”:135.917,\n “ type": "audio_features",\n "id": "1R0a2iXumgCiFb7HEZ7gUE",\n "uri": "spotify:track:1R0a2iXumgCiFb7HEZ7gUE",\n "track_href": "https://api.spotify.com/v1/轨道/1R0a2iXumgCiFb7HEZ7gUE",\n "analysis_url": "https://api.spotify.com/v1/audio-analysis/1R0a2iXumgCiFb7HEZ7gUE",\n "duration_ms": 236413,\n "time_signature": 4\n}" },{"0":"{\n "舞蹈能力": 0.532,\n "能量": 0.623,\n "键": 5,\n "响度": -9.208,\n "模式": 1, \n“语音”:0.0331,\n“声学”:0.538,\n“器乐”:0.0000728,\n“活力”:0.0925,\n“价”:0.403,\n“节奏”:89.937,\n "type": "audio_features",\n "id": "3hUxzQpSfdDqwM3ZTFQY0K",\n "uri": "spotify:track:3hUxzQpSfdDqwM3ZTFQY0K",\n "track_href": "https://api.spotify.com/v1 /tracks/3hUxzQpSfdDqwM3ZTFQY0K",\ n "analysis_url": "https://api.spotify.com/v1/audio-analysis/3hUxzQpSfdDqwM3ZTFQY0K",\n "duration_ms": 261923,\n "time_signature": 4\n}"},{"0":"{\n "danceability": 0.753,\n "energy": 0.678,\n "key": 5,\n "loudness": -5.421,\n "mode": 1,\n "speechiness": 0.0644,\n "acousticness": 0.0850,\n "instrumentalness": 0.00000164,\n "liveness": 0.130,\n "valence": 0.583,\n "tempo": 96.006,\n "type": "audio_features",\n "id": "1p80LdxRV74UKvL8gnD7ky",\n "uri": "spotify:track:1p80LdxRV74UKvL8gnD7ky",\n "track_href": "https://api.spotify.com/v1/tracks/1p80LdxRV74UKvL8gnD7ky",\n "analysis_url": "https://api.spotify.com/v1/audio-analysis/1p80LdxRV74UKvL8gnD7ky",\n "duration_ms": 231827,\n "time_signature": 4\n}"},{"0":"{\n "danceability": 0.552,\n "energy": 0.702,\n "key": 9,\n "loudness": -5.707,\n "mode": 1,\n "speechiness": 0.157,\n "acousticness": 0.117,\n "instrumentalness": 0.0000206,\n "liveness": 0.105,\n "valence": 0.564,\n "tempo": 169.994,\n "type": "audio_features",\n "id": "1BxfuPKGuaTgP7 n "analysis_url": "https://api.spotify.com/v1/audio-analysis/3hUxzQpSfdDqwM3ZTFQY0K",\n "duration_ms": 261923,\n "time_signature": 4\n}"},{"0" :"{\n "舞蹈能力": 0.753,\n "能量": 0.678,\n "键": 5,\n "响度": -5.421,\n "模式": 1,\n "演讲能力": 0.0644,\n“声学”:0.0850,\n“乐器”:0.00000164,\n“活跃度”:0.130,\n“价”:0.583,\n“节奏”:96.006,\n“类型”:“audio_features ",\n "id": "1p80LdxRV74UKvL8gnD7ky",\n "uri": "spotify:track:1p80LdxRV74UKvL8gnD7ky",\n "track_href": "https://api.spotify.com/v1/tracks/1p80LdxRV74UKvL8gnD7ky", \n "analysis_url": "https://api.spotify.com/v1/audio-analysis/1p80LdxRV74UKvL8gnD7ky",\n "duration_ms": 231827,\n "time_signature": 4\n}"},{"0 ":"{\n "舞蹈能力": 0.552,\n "能量": 0.702,\n "键": 9,\n "响度": -5.707,\n "模式": 1,\n "演讲能力" :0.157,\n“声学”:0.117,\n“器乐”:0.0000206,\n“活跃度”:0.105,\n“价”:0.564,\n“节奏”:169.994,\n“类型”:“ audio_features",\n "id": "1BxfuPKGuaTgP7 aM0Bbdwr",\n "uri": "spotify:track:1BxfuPKGuaTgP7aM0Bbdwr",\n "track_href": "https://api.spotify.com/v1/tracks/1BxfuPKGuaTgP7aM0Bbdwr",\n "analysis_url": "https://api.spotify.com/v1/audio-analysis/1BxfuPKGuaTgP7aM0Bbdwr",\n "duration_ms": 178427,\n "time_signature": 4\n}"} aM0Bbdwr",\n "uri": "spotify:track:1BxfuPKGuaTgP7aM0Bbdwr",\n "track_href": "https://api.spotify.com/v1/tracks/1BxfuPKGuaTgP7aM0Bbdwr",\n "analysis_url": "https: //api.spotify.com/v1/audio-analysis/1BxfuPKGuaTgP7aM0Bbdwr",\n "duration_ms": 178427,\n "time_signature": 4\n}"}

i'd be pretty thankful if someone could help me with this:)如果有人可以帮助我,我将非常感激:)

I can read it correctly:我可以正确阅读:

stri = """{ "danceability" : 0.615, "energy" : 0.534, "key" : 9, "loudness" : -6.719, "mode" : 0, "speechiness" : 0.0386, "acousticness" : 0.106, "instrumentalness" : 0.0000176, "liveness" : 0.0607, "valence" : 0.193, "tempo" : 135.917, "type" : "audio_features", "id" : "1R0a2iXumgCiFb7HEZ7gUE", "uri" : "spotify:track:1R0a2iXumgCiFb7HEZ7gUE", "track_href" : "https://api.spotify.com/v1/tracks/1R0a2iXumgCiFb7HEZ7gUE", "analysis_url" : "https://api.spotify.com/v1/audio-analysis/1R0a2iXumgCiFb7HEZ7gUE", "duration_ms" : 236413, "time_signature" : 4},{ "danceability" : 0.532, "energy" : 0.623, "key" : 5, "loudness" : -9.208, "mode" : 1, "speechiness" : 0.0331, "acousticness" : 0.538, "instrumentalness" : 0.0000728, "liveness" : 0.0925, "valence" : 0.403, "tempo" : 89.937, "type" : "audio_features", "id" : "3hUxzQpSfdDqwM3ZTFQY0K", "uri" : "spotify:track:3hUxzQpSfdDqwM3ZTFQY0K", "track_href" : "https://api.spotify.com/v1/tracks/3hUxzQpSfdDqwM3ZTFQY0K", "analysis_url" : "https://api.spotify.com/v1/audio-analysis/3hUxzQpSfdDqwM3ZTFQY0K", "duration_ms" : 261923, "time_signature" : 4}"""
pd.read_json(stri, lines=True)

Output: Output:

在此处输入图像描述

It looks like your json file might have an extra key layer that is preventing pandas parsing the layer you want.看起来您的 json 文件可能有一个额外的关键层,阻止 pandas 解析您想要的层。 If you don't want to/can't edit the json file, this should work as a quick workaround:如果您不想/不能编辑 json 文件,这应该是一个快速的解决方法:

temp = pd.read_json("my.json", orient='columns')
# Get only the 0 column as a list of dictionaries
j = temp[0].to_list()
# Generate the dataframe from the list of dictionaries
df = pd.DataFrame(j)

(If this doesn't work, or in the future, please post a sample of your json data to make it easier for people to help you.) (如果这不起作用,或者将来,请发布您的 json 数据样本,以便人们更容易地帮助您。)

hi i hope to help you:嗨,我希望能帮助你:

i should see your file to help you but use this link我应该看到你的文件来帮助你,但使用这个链接

or try this run this:或者试试这个运行这个:

import pandas as pd
df = pd.json_normalize(json.loads('/content/Top100.json')) 
print(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM