anu krishna
anu krishna

Reputation: 3

iterate over a dictionary of list of dictionaries in pandas dataframe

How to iterate over a dictionary of list of dictionaries in pandas dataframe one of the column is Response.

Response = {"query":"hi","intents":[{"intent":"greeting","score":0.941468239},{"intent":"sentinel","score":7.298465E-06},{"intent":"analyticsPage","score":3.77489937E-06},{"intent":"KaaraServices","score":0.0251461584},{"intent":"goodBye","score":0.357869864},{"intent":"servicesPage","score":2.3839857E-05},{"intent":"PredAnalytics","score":3.21742641E-06},{"intent":"ykaara","score":0.006888155},{"intent":"creator","score":0.061054837}],"entities":[]}, 

I want to get the intent with the maximum score.

Upvotes: 0

Views: 1606

Answers (3)

AndreyF
AndreyF

Reputation: 1838

You can turn the value (the list of dicts) of 'intents' into a DataFrame and then use argmax:

df = pd.DataFrame(Response["intents"])

Its output is:

          intent     score
0       greeting  0.941468
1       sentinel  0.000007
2  analyticsPage  0.000004
3  KaaraServices  0.025146
4        goodBye  0.357870
5   servicesPage  0.000024
6  PredAnalytics  0.000003
7         ykaara  0.006888
8        creator  0.061055

And then for the max:

df.iloc[df['score'].argmax(),:]

this will return:

intent    greeting
score     0.941468
Name: 0, dtype: object

Upvotes: 3

Narendra
Narendra

Reputation: 1529

Try this:-

Response = {"query":"hi","intents":[{"intent":"greeting","score":0.941468239},{"intent":"sentinel","score":7.298465E-06},\
{"intent":"analyticsPage","score":3.77489937E-06},{"intent":"KaaraServices","score":0.0251461584},\
{"intent":"goodBye","score":0.357869864},{"intent":"servicesPage","score":2.3839857E-05},\
{"intent":"PredAnalytics","score":3.21742641E-06},{"intent":"ykaara","score":0.006888155},\
{"intent":"creator","score":0.061054837}],"entities":[]},
max_score = []
for i in Response:
    for j in i['intents']:
        max_score.append(j['score'])
print(max(max_score)) #your expected outpu

Upvotes: 0

nchen24
nchen24

Reputation: 502

I'm sure there's also pandas specific way(s) to do it, but another option is to extract the dictionary and sort it like a generic dictionary.

from operator import itemgetter

# Get pandas frame from somewhere
response = getresponse()

# Convert list of dictionary to dictionary
d = {}
for item in response["intents"]:
    d[item["intent"]] = item["score"]

# Sort dictionary, get highest scoring element
sorted(d.items(), key=itemgetter(1), reverse=True)[0]

Upvotes: 0

Related Questions