- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在分析一个 json 文件,我想将嵌套的 json 输入文件转换为 python 中的平面数据框。有没有任何 python 方法可以管理这个?或者我应该创建一个自定义函数来做到这一点?您能提供一个例子来解决这个问题吗?
我尝试了 json_normalize 函数,并且还尝试了另一种解决方案:嵌套 for 语句在每个嵌套级别逐个元素检索
d = pd.read_json('test 1.json', lines=True)
from pandas.io.json import json_normalize
d2=json_normalize(d['track])
我尝试过的第二个选项:
for index, row in d.iterrows():
for element in row['track']:
if element == "features":
print(row['track']['features'])
json文件内容:
{ "_id" : { "$oid" : "5b9058462f38434ab0d85cd3" }, "user_day_code" : "ead1db07fa526e19fe237115d5516fbdc5acb99057b885e8f662a147990b3c4b", "idplug_base" : 5, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.7073786, 40.4237274997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE SAN BERNARDO 38,Madrid", "speed" : 1.75, "secondsfromstart" : 205 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.709896, 40.4191897997222 ] }, "type" : "Feature", "properties" : { "var" : "28013,ES,Madrid,Madrid,CUSTA SANTO DOMINGO 6,Madrid", "speed" : 4.63, "secondsfromstart" : 85 } } ] }, "user_type" : 1, "idunplug_base" : 17, "travel_time" : 263, "idunplug_station" : 40, "ageRange" : 0, "idplug_station" : 16, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85ce9" }, "user_day_code" : "420d9e220bd8816681162e15e9afcb1c69c5a756090728701083c5c0b23502f2", "idplug_base" : 12, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.7022001, 40.4052982997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,GTA EMBAJADORES,Madrid", "speed" : 0.33, "secondsfromstart" : 351 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.698618, 40.4061700997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,RONDA ATOCHA 30,Madrid", "speed" : 6.36, "secondsfromstart" : 291 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.6949231, 40.4072785997222 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,RONDA ATOCHA,Madrid", "speed" : 4.77, "secondsfromstart" : 231 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.6920543, 40.4081501 ] }, "type" : "Feature", "properties" : { "var" : "28012,ES,Madrid,Madrid,PLAZA EMPERADOR CARLOS V 1,Madrid", "speed" : 4.38, "secondsfromstart" : 170 } } ] }, "user_type" : 1, "idunplug_base" : 26, "travel_time" : 382, "idunplug_station" : 85, "ageRange" : 2, "idplug_station" : 52, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28009" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85ced" }, "user_day_code" : "780f5c8157efe8e6dca44dbd689817d4b126364fca917f0e668bad9e7bf96939", "idplug_base" : 1, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.69610249972222, 40.427829 ] }, "type" : "Feature", "properties" : { "var" : "28004,ES,Madrid,Madrid,PLAZA ALONSO MARTINEZ,Madrid", "speed" : 6.22, "secondsfromstart" : 200 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.69482799972222, 40.4282634997222 ] }, "type" : "Feature", "properties" : { "var" : "28010,ES,Madrid,Madrid,CALLE FERNANDO EL SANTO 4,Madrid", "speed" : 0, "secondsfromstart" : 140 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.69164359972222, 40.4280088 ] }, "type" : "Feature", "properties" : { "var" : "28010,ES,Madrid,Madrid,CALLE FERNANDO EL SANTO 20,Madrid", "speed" : 5.05, "secondsfromstart" : 80 } } ] }, "user_type" : 1, "idunplug_base" : 11, "travel_time" : 305, "idunplug_station" : 109, "ageRange" : 4, "idplug_station" : 58, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28004" }
{ "_id" : { "$oid" : "5b9058462f38434ab0d85cee" }, "user_day_code" : "a225ab7b4b74954cd9fbe8cc2ec63390cd04e92cdd1a2fe1e58d42faea082b21", "idplug_base" : 1, "track" : { "type" : "FeatureCollection", "features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ -3.72050759972222, 40.4277548 ] }, "type" : "Feature", "properties" : { "var" : "28008,ES,Madrid,Madrid,PASEO PINTOR ROSALES 49P,Madrid", "speed" : 0.86, "secondsfromstart" : 258 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.717881, 40.4274713 ] }, "type" : "Feature", "properties" : { "var" : "28008,ES,Madrid,Madrid,CALLE QUINTANA 17,Madrid", "speed" : 6.75, "secondsfromstart" : 199 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.7142441, 40.4297779997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE SERRANO JOVER 4D,Madrid", "speed" : 7.08, "secondsfromstart" : 139 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.71240559972222, 40.4341422997222 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE FERNANDO EL CATOLICO 47A,Madrid", "speed" : 5.25, "secondsfromstart" : 79 } }, { "geometry" : { "type" : "Point", "coordinates" : [ -3.7089558, 40.4340593 ] }, "type" : "Feature", "properties" : { "var" : "28015,ES,Madrid,Madrid,CALLE FERNANDO EL CATOLICO 21,Madrid", "speed" : 5.61, "secondsfromstart" : 19 } } ] }, "user_type" : 1, "idunplug_base" : 1, "travel_time" : 262, "idunplug_station" : 168, "ageRange" : 4, "idplug_station" : 120, "unplug_hourTime" : { "$date" : "2018-09-01T01:00:00.000+0200" }, "zip_code" : "28015" }
实际结果:选项 1:不起作用,数据框保持嵌套状态。选项2:非常复杂的方式
预期结果:包含初始 json 的所有元素的平面数据框。
预期的平面数据框示例:
_id user_day_code idplug_base track coordinates var speed secondsfromstart user_type idunplug_base ...
5b9058462f38434ab0d85ce9 420d9e220bd8816681162e15e9afcb1c69c5a756090728701083c5c0b23502f2 12 1 -3.7022001, 40.4052982997222 28012,ES,Madrid,Madrid,GTA EMBAJADORES,Madrid 0.33 351 1 26 ...
5b9058462f38434ab0d85ce9 420d9e220bd8816681162e15e9afcb1c69c5a756090728701083c5c0b23502f2 12 2 -3.698618, 40.4061700997222 28012,ES,Madrid,Madrid,RONDA ATOCHA 30,Madrid 6.36 291 1 26 ...
...
最佳答案
您有一个 JSON 行文件。将其作为字典列表读入,然后调用 json_normalize。您需要自己进行一定程度的解除嵌套。
def update(a, b):
a.update(b)
return a
l = pd.read_json('test 1.json', lines=True).to_dict('r')
json_normalize([update(y, x) for x in l for y in x.pop('track')['features']])
<小时/>
首先,使用 pd.read_json
和 lines=True
参数读取 JSON 行文件。使用 to_dict(orient='records')
将数据帧重新转换为字典列表。
l = pd.read_json('test 1.json', lines=True).to_dict('r')
接下来,对于 l
中的每个子列表 x
,取消嵌套 x['tracks']
中的数据及其元数据。
例如,
import copy
dct = copy.deepcopy(l[0])
x = dct.pop('track')['features'][0]
r = {**x, **dct}
# {'_id': {'$oid': '5b9058462f38434ab0d85cd3'},
# 'ageRange': 0,
# 'geometry': {'coordinates': [-3.7073786, 40.4237274997222], 'type': 'Point'},
# 'idplug_base': 5,
# 'idplug_station': 16,
# ...
# 'user_day_code': 'ead1db07fa526e19fe237115d5516fbdc5acb99057b885e8f662a147990b3c4b',
# 'user_type': 1,
# 'zip_code': ''}
我们生成这些扁平子字典的列表,并且 json_normalize
可以处理其余部分:
json_normalize([r])
_id.$oid ageRange ... user_type zip_code
0 5b9058462f38434ab0d85cd3 0 ... 1
json_normalize([r]).iloc[0].T
_id.$oid 5b9058462f38434ab0d85cd3
ageRange 0
geometry.coordinates [-3.7073786, 40.4237274997222]
geometry.type Point
...
user_day_code ead1db07fa526e19fe237115d5516fbdc5acb99057b885...
user_type 1
zip_code
Name: 0, dtype: object
关于python - Pandas json_normalize 不会展平所有嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54242716/
pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能。有什么不同吗? 最佳答案 pivot_table没有 normalize争论,不幸的是。 在 crosstab
我能找到的最接近的答案似乎太复杂:How I can create an interval column in pandas? 如果我有一个如下所示的 pandas 数据框: +-------+ |
这是我用来将某一行的一列值移动到同一行的另一列的当前代码: #Move 2014/15 column ValB to column ValA df.loc[(df.Survey_year == 201
我有一个以下格式的 Pandas 数据框: df = pd.DataFrame({'a' : [0,1,2,3,4,5,6], 'b' : [-0.5, 0.0, 1.0, 1.2, 1.4,
所以我有这两个数据框,我想得到一个新的数据框,它由两个数据框的行的克罗内克积组成。正确的做法是什么? 举个例子:数据框1 c1 c2 0 10 100 1 11 110 2 12
TL;DR:在 pandas 中,如何绘制条形图以使其 x 轴刻度标签看起来像折线图? 我制作了一个间隔均匀的时间序列(每天一个项目),并且可以像这样很好地绘制它: intensity[350:450
我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1): Time1 Time2
从这个 df 去的正确方法是什么: >>> df=pd.DataFrame({'a':['jeff','bob','jill'], 'b':['bob','jeff','mike']}) >>> df
我想按周从 Pandas 框架中的列中累积计算唯一值。例如,假设我有这样的数据: df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,
数据透视表的表示形式看起来不像我在寻找的东西,更具体地说,结果行的顺序。 我不知道如何以正确的方式进行更改。 df示例: test_df = pd.DataFrame({'name':['name_1
我有一个数据框,如下所示。 Category Actual Predicted 1 1 1 1 0
我有一个 df,如下所示。 df: ID open_date limit 1 2020-06-03 100 1 2020-06-23 500
我有一个 df ,其中包含与唯一值关联的各种字符串。对于这些唯一值,我想删除不等于单独列表的行,最后一行除外。 下面使用 Label 中的各种字符串值与 Item 相关联.所以对于每个唯一的 Item
考虑以下具有相同名称的列的数据框(显然,这确实发生了,目前我有一个像这样的数据集!:() >>> df = pd.DataFrame({"a":range(10,15),"b":range(5,10)
我在 Pandas 中有一个 DF,它看起来像: Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2
如何减去两列之间的时间并将其转换为分钟 Date Time Ordered Time Delivered 0 1/11/19 9:25:00 am 10:58:00 am
我试图理解 pandas 中的下/上百分位数计算,但有点困惑。这是它的示例代码和输出。 test = pd.Series([7, 15, 36, 39, 40, 41]) test.describe(
我有一个多索引数据框,如下所示: TQ bought HT Detailed Instru
我需要从包含值“低”,“中”或“高”的数据框列创建直方图。当我尝试执行通常的df.column.hist()时,出现以下错误。 ex3.Severity.value_counts() Out[85]:
我试图根据另一列的长度对一列进行子串,但结果集是 NaN .我究竟做错了什么? import pandas as pd df = pd.DataFrame([['abcdefghi','xyz'],
我是一名优秀的程序员,十分优秀!