Reputation: 1293
I have the same format of nested dictionaries within a list. I am trying to iterate through every element and find the number of time we have values for summary
as through the API
and also count the number of assignees for through the API
.
[{'id': '101',
'type': 'log_file',
'summary': 'Escalated to Mark through the API',
'assignees': [{'id': 'xyz',
'type': 'user_reference',
'summary': 'Mark'}]},
{'id': '102',
'type': 'log_file',
'summary': 'Escalated to Kevin by SMS',
'assignees': [{'id': 'abc',
'type': 'user_reference',
'summary': 'Kevin'}]},
{'id': '103',
'type': 'log_file',
'summary': 'Escalated to Scott through the API',
'assignees': [{'id': 'pqr',
'type': 'user_reference',
'summary': 'Scott'}]}]
In the sample above, I expect a count of 2 for number of times through the API
has returned for summary
and the assignee value as Mark and Scott
as these two different people have been assigned.
Upvotes: 2
Views: 42
Reputation: 4921
Make a Pandas DataFrame:
df=pd.DataFrame(dd)
Count the number of appearances and put assignees names in a separate column:
df['summary'].str.contains('through the API').sum()
df['names'] = pd.Series([df.iloc[s,3][0]['summary'] for s in np.arange(0,df.shape[0])])
Upvotes: 1
Reputation: 193
You can just loop through the list. Here is my example:
data = [{'id': '101', 'type': 'log_file', 'summary': 'Escalated to Mark through the API', 'assignees': [{'id': 'xyz', 'type': 'user_reference', 'summary': 'Mark'}]}, {'id': '102', 'type': 'log_file', 'summary': 'Escalated to Kevin by SMS', 'assignees': [{'id': 'abc', 'type': 'user_reference', 'summary': 'Kevin'}]}, {'id': '103', 'type': 'log_file', 'summary': 'Escalated to Scott through the API', 'assignees': [{'id': 'pqr', 'type': 'user_reference', 'summary': 'Scott'}]}]
people = [sum["assignees"][0]["summary"] for sum in data if "through the API" in sum["summary"]] #This will return ["Mark", "Scott"]
number_of_assignees = len(people) #This will return 2
Upvotes: 1