Untitled
def extract_highest_score_label(sentiments): column_names = ['negative', 'neutral', 'positive'] total_scores = [] for senti in sentiments: scores = [] for sentiment in literal_eval(senti): for c in column_names: if sentiment['label'] == c: scores.append({c: sentiment['score']}) total_scores.append(scores) flattened_data = [{k: v for d in item for k, v in d.items()} for item in total_scores] df_sentiments = pd.DataFrame(flattened_data).reindex(columns=column_names) return df_sentiments if matching_places is not None: matching_places = matching_places.reset_index(drop=True) sentiment_values = extract_highest_score_label(matching_places["sentiment"]) df_concat = pd.concat([matching_places, sentiment_values], axis=1) df_concat = df_concat.drop(columns=['sentiment']) df_concat = df_concat.groupby('place_name').mean() df_concat['count'] = matching_places['place_name'].value_counts() print(df_concat) else: print("No matching places found.")
Leave a Comment