我有一个如下所示的数据框(列名 - 日期、错误消息、消息)data = """Date|Error|message 26/11/19 | unauthorized access | {"eventVersion":"1.05","userIdentity":"type":"IAMUser","principalId":"AIDAIETZDDDVS36MMCHPS","arn":"arn:aws:iam::819490967212:user/IAMAdmin","accountId":"819490967212","accessKeyId":"ASIA35TLXZ2WPIBTBWP2","userName":"IAMAdmin","sessionContext":{"sessionIssuer":{},"webIdFederationData":{},"attributes":{"mfaAuthenticated":"false","creationDate":"2019-12-19T03:14:04Z"}}"""from io import StringIOdf = pd.read_csv(StringIO(data),sep='|)print(df)Date Error message0 26/11/19 unauthorized access {"eventVersion":"1.05","userIdentity":"type":...消息列的每一行都有类似json格式的数据。如何从“消息”列中检索某些键及其各自的值("userName", "account ID"),以便这些键可以成为新列。尝试使用 python 但无法检索
2 回答
森栏
TA贡献1810条经验 获得超5个赞
您可以使用 json 或 pandas
import json
#some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
或者您可以使用 pandas.read_json() 它以相同的方式工作,但您应该使用方向。在这里查看:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html
慕码人8056858
TA贡献1803条经验 获得超6个赞
df['userName'] = df['message'].apply(lambda x: x.get('userName')) df['account'] = df['message'].apply(lambda x: x.get('account'))
添加回答
举报
0/150
提交
取消