Search code examples
pandasibm-watsontone-analyzer

How to display output of IBM tone analyzer in pandas frame


My dataset has following features: "description", "word_count", "char_count", "stopwords". The feature "description" has datatype as string which contains some text. I am doing IBM tone_analysis on this feature which gives me correct output and looks like this:

[{'document_tone': {'tones': [{'score': 0.677676,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'}]}},
 {'document_tone': {'tones': [{'score': 0.620279,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'}]}},    

The code for above is given as below:

result =[]
for i in new_df['description']:
   tone_analysis = ta.tone(
       {'text': i},
     #  'application/json'
   ).get_result()
   result.append(tone_analysis)

I need to keep the above output in pandas data frame.


Solution

  • Use lambda function in Series.apply:

    new_df['new'] = new_df['description'].apply(lambda i: ta.tone({'text': i}).get_result())
    

    EDIT:

    def f(i):
        x = ta.tone({'text': i}).get_result()['document_tone']['tones']
        return pd.Series(x[0])
    
    
    new_df = new_df.join(new_df['description'].apply(f).drop('tone_id', axis=1))
    print (new_df)
    

    If need also remove description column:

    new_df = new_df.join(new_df.pop('description').apply(f).drop('tone_id', axis=1))