我有一个json文件,该文件已加载到变量“ tags”中。它包含ID和相应的值。对于每个ID,我想将值添加到数据框的列中。如下面的代码所示,这对于值“ X”,“ Y”,“ Z”的效果很好。
我的json文件的格式如下: {“ 1”:[“ X”],“ 2”:[“ Z”],“ 3”:[“ Y”],“ 4”:[“ X”]}
tags = json.load(f)
tags.items()
for key, value in tags.items():
try:
if 'X' in value:
df.at[key, 'X'] = True
if 'Y' in value:
df.at[key, 'Y'] = True
if 'Z' in value:
df.at[key, 'Z'] = True
else
df.at[key, 'Q'] = True
except KeyError:
pass
我的问题是我在json文件中缺少一些值,并且想在数据帧的Q列中将它们替换为“ True”。有什么想法吗?
答案 0 :(得分:0)
这假定丢失的数据像“ 5”:[“”]
from io import StringIO
import json
import pandas as pd
# Assume the missing value format
text = """
{"1": ["X"], "2": ["Z"], "3": ["Y"], "4": ["X"], "5":[""]}
"""
# The index option makes the dataframe row-oriented with index values
df = pd.read_json(StringIO(text), orient="index")
# Provide a maningful column name
df.columns = ["variable"]
# Assign the Q value to missing
df['variable'][df['variable'] == ""] = "Q"
# Pandas get_dummies does the transformation - (need pandas 0.24 for the dtype)
pd.get_dummies(df, prefix="", prefix_sep="", dtype=bool)
结果
Q X Y Z
1 False True False False
2 False False False True
3 False False True False
4 False True False False
5 True False False False