当键和值包含在现有字段的值中时,读取它们



我有一个数据提取,其中包含以下格式的几个JSON字符串:

{'assignedTo': 'a5060ed2', 'automated': 'Not Automated', 'build': None, 'configurationId': 123, 
      'configurationName': 'Package 1.0', 'lastResultState': 1, 'lastRunBy': '', 'lastRunDuration': 0, 
      'mostRecentResultOutcome': 2, 'mostRecentRunId': 1234, 'outcome': 'Passed', 'state': 2, 
      'suiteId': 1234, 'suiteName': 'Name', 'testCaseId': 12345, 'testPointId': 12345, 'tester': 'Fred Smith', 
      'workItemProperties': [{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'}, 
                             {'Key': 'System.IterationPath', 'Value': 'PathPath'}, 
                             {'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'}, 
                             {'Key': 'System.ChangedBy', 'Value': 'Fred Smith'}, 
                             {'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]}

我已经能够遍历这些并在 Pandas 数据帧中显示它们,将每个字符串附加为新行,但我遇到了一个问题。我的 json 字符串中有一个字段列表:

assignedTo
etc
workItemProperties < - this is the last field in the list

最后一个字段"workItemProperties"的值如下:

[{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'}, 
                             {'Key': 'System.IterationPath', 'Value': 'PathPath'}, 
                             {'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'}, 
                             {'Key': 'System.ChangedBy', 'Value': 'Fred Smith'}, 
                             {'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]

我希望能够在表中显示该值中保存的字段,因此我的字段列表如下所示:

assignedTo
…
workItemProperties
System.Id
System.Title
System.IterationPath
Etc

是否有可能让熊猫从workItemProperties的值中拾取并识别这些"子"字段和值?或者我是否必须进行某种进一步的字符串提取/操作?

您可以使用

json_normalize

前任:

from pandas.io.json import json_normalize
data = {'assignedTo': 'a5060ed2', 'automated': 'Not Automated', 'build': None, 'configurationId': 123, 
      'configurationName': 'Package 1.0', 'lastResultState': 1, 'lastRunBy': '', 'lastRunDuration': 0, 
      'mostRecentResultOutcome': 2, 'mostRecentRunId': 1234, 'outcome': 'Passed', 'state': 2, 
      'suiteId': 1234, 'suiteName': 'Name', 'testCaseId': 12345, 'testPointId': 12345, 'tester': 'Fred Smith', 
      'workItemProperties': [{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'}, 
                             {'Key': 'System.IterationPath', 'Value': 'PathPath'}, 
                             {'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'}, 
                             {'Key': 'System.ChangedBy', 'Value': 'Fred Smith'}, 
                             {'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]}

df = json_normalize(data, "workItemProperties", ['lastRunDuration', 'tester', 'testPointId', 'lastResultState', 'configurationId', 'mostRecentRunId', 'suiteName', 'state', 'testCaseId', 'assignedTo', 'configurationName', 'suiteId', 'build', 'mostRecentResultOutcome', 'automated', 'outcome', 'lastRunBy'])
df["workItemProperties"] = df.pop("Key")
df.drop(["Value"], inplace=True, axis=1)
print(df)

输出:

   lastRunDuration  mostRecentResultOutcome      tester  configurationId  
0                0                        2  Fred Smith              123   
1                0                        2  Fred Smith              123   
2                0                        2  Fred Smith              123   
3                0                        2  Fred Smith              123   
4                0                        2  Fred Smith              123   
5                0                        2  Fred Smith              123   
   mostRecentRunId suiteName  testCaseId  lastResultState  state  suiteId  
0             1234      Name       12345                1      2     1234   
1             1234      Name       12345                1      2     1234   
2             1234      Name       12345                1      2     1234   
3             1234      Name       12345                1      2     1234   
4             1234      Name       12345                1      2     1234   
5             1234      Name       12345                1      2     1234   
  build  testPointId      automated configurationName outcome assignedTo  
0  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
1  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
2  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
3  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
4  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
5  None        12345  Not Automated       Package 1.0  Passed   a5060ed2   
  lastRunBy                   workItemProperties  
0                                      System.Id  
1                                   System.Title  
2                           System.IterationPath  
3                             System.ChangedDate  
4                               System.ChangedBy  
5            Microsoft.VSTS.TCM.AutomationStatus  

接受的答案非常有效,但有人向我建议的另一个选项也有效:

for item in df['workItemProperties']:
    key = item['Key']
    df[key] = item['Value']
del dfheader['workItemProperties']
table = pd.DataFrame(df,index=[0])

这会将子字段和值完全展平为包含其余数据的列。

最新更新