Unable to access dictionary within multiple nested dictionaries because of tuple error

user10007925 :

I have a dataframe called output which looks like -

output
Out[48]: 
({'results': [{'alternatives': [{'confidence': 0.82,
      'transcript': 'thank you for calling a AA health insurance my name is Dick right the pleasure of speaking with '}],
    'final': True},
   {'alternatives': [{'confidence': 0.57, 'transcript': 'hi Nick this is '}],
    'final': True},
   {'alternatives': [{'confidence': 0.78,
      'transcript': 'hi Julie I think we talked earlier we did '}],
    'final': True},
  {'alternatives': [{'confidence': 0.86,
      'transcript': "thing else comes up or you have any questions just don't hesitate to call us okay okay thank you so much yeah you're very welcome you have a great rest your day okay you too bye bye "}],
    'final': True}],
  'result_index': 0},)

I am trying to access only the 'transcript' and converting this to a dataframe to a csv. I tried -

output.to_csv("script.csv")
Traceback (most recent call last):

  File "<ipython-input-44-85a7c839323b>", line 1, in <module>
    output.to_csv("script.csv")

AttributeError: 'tuple' object has no attribute 'to_csv'

I also tried to just access the transcript but I got the same below error-

print(output['results'][0]['alternatives'][0]['transcript'])

Traceback (most recent call last):

  File "<ipython-input-49-03a8e1a518ee>", line 1, in <module>
    print(output['results'][0]['alternatives'][0]['transcript'])

TypeError: tuple indices must be integers or slices, not str

How do I escape this error?

Serge Ballesta :

You can build a pandas DataFrames from a list of dictionaries. Assuming you only need the inner most elements, you can use a comprehension to get it:

df = pd.DataFrame([i for elts in output for alts in elts['results'] for i in alts['alternatives']])

You will get the following DataFrame:

   confidence                                         transcript
0        0.82  thank you for calling a AA health insurance my...
1        0.57                                   hi Nick this is 
2        0.78         hi Julie I think we talked earlier we did 
3        0.86  thing else comes up or you have any questions ...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10167&siteId=1