我在从JSON中获取值并存储在Dataframe中时遇到了麻烦。我的Json看起来像
{
"issues": [
{
"expand": "operations",
"id": "1",
"fields": {
"customfield_100": [
{
"self": "https://url1001",
"value": "Mobile",
"id": "1001",
"disabled": "false"
}
]
}
},
{
"expand": "operations",
"id": "2",
"fields": {
"customfield_100": [
{
"self": "https://url1002",
"value": "Desktop",
"id": "1002",
"disabled": false
},
{
"self": "https://url1001",
"value": "Mobile",
"id": "1001",
"disabled": false
}
]
}
},
{
"expand": "operations",
"id": "3",
"fields": {
"customfield_100": [
{
"self": "https://url1003",
"value": "ios",
"id": "1002",
"disabled": false
}
]
}
},
{
"expand": "operations",
"id": "4",
"fields": {
"customfield_100": [
{
"self": "https://url1002",
"value": "Desktop",
"id": "1002",
"disabled": false
},
{
"self": "https://url1001",
"value": "Mobile",
"id": "1001",
"disabled": false
},
{
"self": "https://url1003",
"value": "ios",
"id": "1003",
"disabled": false
}
]
}
}
]
}
下面是我的部分代码
df2=pd.dataframe()
d=pd.json_normalize(json.loads(df1['customfield_100'].to_json(orient='record')))
filtered_component=[]
for index in range(len(issues.id)):
if((pd.json_normalize(df1['customfield_100'][index])).size>0):
filtered_component.append(d[0][index]['value']
else:
filtered_component.append('No Component')
df2['Component']=filterd_component
当我列出df2 ['Component ']时,我得到以下输出
'Mobile'
'Desktop'
'ios'
'Desktop'
我希望我的输出是这样的(当我列出df2 [组件]),即如果customfield_100有多个值,那么我希望这些值被分隔;.我不确定循环/代码应该如何编写
'Mobile'
'Desktop';'Mobile'
'ios'
'Desktop';'Mobile';'ios'
2条答案
按热度按时间ecbunoof1#
另一种可能的解决方案:
在哪里
输出:
stszievb2#
如果
data
包含解析后的Json数据,则可以执行以下操作:这将打印:
然后,您可以按
main_id
分组,例如:这将打印: