2017-03-08 12 views
5

से स्टॉपवर्ड को हटा रहा है, मैं अपनी फ़ाइल में डेटा कॉलम से स्टॉपवर्ड को हटाना चाहता हूं। मैंने अंतिम उपयोगकर्ता बोलने के लिए लाइन को फ़िल्टर किया। लेकिन यह usertext.apply(lambda x: [word for word in x if word not in stop_words]) के साथ स्टॉपवर्ड को फ़िल्टर नहीं करता है, मैं गलत क्या कर रहा हूं?फ़ाइल

import pandas as pd 
from stop_words import get_stop_words 
df = pd.read_csv("F:/textclustering/data/cleandata.csv", encoding="iso-8859-1") 
usertext = df[df.Role.str.contains("End-user",na=False)][['Data','chatid']] 
stop_words = get_stop_words('dutch') 
clean = usertext.apply(lambda x: [word for word in x if word not in stop_words]) 
print(clean) 
+0

पहले y कहां 1) 'stop_words' प्रिंट करें, 2)' clean = usertext.apply (lambda x: []) 'यह देखने के लिए कि क्या यह सभी शब्दों को हटा देता है? (बस परीक्षण करने के लिए) –

+0

डेटा [] चैटिड [] dtype: ऑब्जेक्ट ['एन', 'अल', 'एलिस', 'अलस', 'altijd', 'andere', 'ben', 'bij' , 'दायर', 'दान', 'डेटा', 'डी', 'डेर', 'डीज', 'मर', 'डिट', 'डच', 'डॉन', 'दरवाजा', 'डस', ' ईन ',' ईन्स ',' एन ',' एर ',' जीई ',' जीन ',' गीवेस्ट ',' हायर ',' था ',' हेब ',' हेबबेन ',' हेफ्ट ',' हेम ' , 'हेट', 'हायर', 'हिज', 'हो', 'हुन', 'आईमांड', 'आईट्स', 'ik', 'इन', 'है', 'जे', 'जे', ' कान ',' कोन ',' कुनेन ',' मार ',' मी ',' मीर ',' मेन ',' मेट ',' मिज ​​',' मिज ​​',' मोएट ',' ना ',' नायर ' , 'niet', 'niets', 'nog', 'nu', 'of', 'om', 'omdat', ...] यह – DataNewB

उत्तर

0
clean = usertext.apply(lambda x: x if x not in stop_words else '') 
+0

दोनों का आउटपुट है यदि यह काम करता है, तो मैं ' दक्षता प्राप्त करने के लिए 'stop_words' के लिए' सेट करें। –

+0

मुझे NameError मिलता है: ("नाम 'शब्द' परिभाषित नहीं किया गया है", 'इंडेक्स डेटा पर हुआ') जब मैं इसे चलाता हूं – DataNewB

+0

@DataNewB क्षमा करें यह x होना चाहिए – galaxyan

1

आप अपने रोक शब्दों का एक regex पैटर्न का निर्माण और vectorised str.replace फोन उन्हें हटाने के लिए कर सकते हैं:

In [124]: 
stop_words = ['a','not','the'] 
stop_words_pat = '|'.join(['\\b' + stop + '\\b' for stop in stop_words]) 
stop_words_pat 

Out[124]: 
'\\ba\\b|\\bnot\\b|\\bthe\\b' 

In [125]:  
df = pd.DataFrame({'text':['a to the b', 'the knot ace a']}) 
df['text'].str.replace(stop_words_pat, '') 

Out[125]: 
0   to b 
1  knot ace 
Name: text, dtype: object 
यहाँ

हम एक सूची समझ प्रदर्शन प्रत्येक रोक शब्द के आसपास के एक पैटर्न के निर्माण के लिए '\b' के साथ जो एक ब्रेक है और फिर हम '|'

1

का उपयोग कर सभी शब्द दो मुद्दों:

सबसे पहले, आपके पास stop_words नामक एक मॉड्यूल है और बाद में आप stop_words नामक एक चर बनाते हैं। यह बुरा रूप है।

दूसरा, आप .apply पर एक लैम्ब्डा-फ़ंक्शन पास कर रहे हैं जो कि x पैरामीटर सूची में मूल्य के बजाय सूची होने के लिए चाहता है।

यह है कि df.apply(sqrt) करने के बजाय आप df.apply(lambda x: [sqrt(val) for val in x]) कर रहे हैं।

आप क्या करना चाहिए या तो सूची प्रसंस्करण खुद:

clean = [x for x in usertext if x not in stop_words] 

या आप एक समारोह है कि एक समय में एक शब्द लेता है के साथ, लागू करना चाहिए: जैसा कि

clean = usertext.apply(lambda x: x if x not in stop_words else '') 

@ जीन फ्रैंकोइस फेबर ने एक टिप्पणी में सुझाव दिया है कि यदि आप स्टॉप_वर्ड्स सूची के बजाए सेट हैं तो आप चीजों को गति दे सकते हैं:

from stop_words import get_stop_words 

nl_stop_words = set(get_stop_words('dutch')) # NOTE: set 

usertext = ... 
clean = usertext.apply(lambda word: word if word not in nl_stop_words else '')