2

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Akhter
  • 59
  • 1
  • 6

1 Answers1

0

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))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks alot, it somehow works, the output comes in data frame with new feature as ''new' which has values as: {'document_tone': {'tones': [{'score': 0.677676, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]}} in each row. I need output as 'score' and 'tone_name' both in two features. – Akhter Mar 11 '21 at 09:05
  • The only now issue remaining is that the three new features thus created i.e.: "score", "tone_id", "tone_name" all have same values repeated, i.e.: "0.615352", "tentative", "Tentative" respectively. The values should be different inn each row. – Akhter Mar 11 '21 at 15:13
  • @Akhter - If check `print(result)` it return not repeated values? – jezrael Mar 12 '21 at 04:56
  • When I do print(results), the values are unique. But in pandas frame they are repeated that too it takes last row values which get repeated. The three features i.e."score", "tone_id", "tone_name" have repeated values, rest of the features are fine. I just need "score" and "tone_name", that too unique values in each row. – Akhter Mar 13 '21 at 09:19