считать частоту униграмм вместе с колонкой - PullRequest
0 голосов
/ 01 февраля 2020

У меня есть код, как показано ниже

# importing pandas as pd 
import pandas as pd 

# dictionary of lists 
dict = {'name':["a1", "b2", "c2", "d3"], 
        'degree': ["We explained to customer how correct fees (100) were charged. Account balance was too low", "customer was late in paying fees and we have to charge fine", "customer's credit score was too low and we have to charge higher interest rate", "customer complained a lot and didnt listen to our explanation. I had to escalate the call"], 
        'score':[90, 40, 80, 98]} 

# creating a dataframe from a dictionary  
df = pd.DataFrame(dict) 
print (df)

import nltk
nltk.download("popular")
from string import punctuation
import re


from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer

stopword = stopwords.words('english')
wordnet_lemmatizer = WordNetLemmatizer()


from nltk.stem import SnowballStemmer
snowball_stemmer = SnowballStemmer('english')

!pip install contractions
import contractions

def clean_text(text):
    t = contractions.fix(text)#not working

    t= t.lower().split()
    t = [(re.sub(r'[^a-z ]', '', ch)) for ch in t]#remove everything other than a-z
    t= [wordnet_lemmatizer.lemmatize(word) for word in t]
    t=[snowball_stemmer.stem(word) for word in t]
    t=[word for word in t if word not in stopword]




    return t

df["answer"]=df['degree'].apply(clean_text)

display (df)




name    degree  score   answer
0   a1  We explained to customer how correct fees (100...   90  [explain, custom, correct, fee, , charg, accou...
1   b2  customer was late in paying fees and we have t...   40  [custom, wa, late, pay, fee, charg, fine]
2   c2  customer's credit score was too low and we hav...   80  [custom, credit, score, wa, low, charg, higher...
3   d3  customer complained a lot and didnt listen to ...   98  [custom, complain, lot, listen, explan, escal,...

из недавно созданного столбца, мы можем видеть, что слово custom встречается во всех 4 строках (даже если слово встречается дважды в ячейке, я хотел бы сосчитать это как 1). Поэтому я хотел бы создать фрейм данных, в котором в первом столбце будет отображаться имя, униграмма, оценка, частота униграмм

, поэтому результирующий фрейм данных должен выглядеть следующим образом. Я хочу это для всех униграмм. Как я могу сделать то же самое?

a1 custom 90 4
b2 custom 40 4
c3 custom 80 4
d4 custom 98 4

обновление 1

, если есть способ добавить стандартное отклонение для столбца 3-го столбца, сгруппировав аналогичные значения из 2-го столбца? в этом случае я хотел бы вычислить стандартное отклонение (90, 40, 80 и 98) и отобразить в каждой строке, где столбец 2 имеет значение «custom», и сделать это для всех значений во 2-м столбце

...