我有220万个数据样本分类到超过7500个类别.我正在使用pandas和sckit-learn of python这样做.
以下是我的数据集示例
itemid description category
11802974 SPRO VUH3C1 DIFFUSER VUH1 TRIPLE Space heaters Architectural Diffusers
10688548 ANTIQUE BRONZE FINISH PUSHBUTTON switch Door Bell Pushbuttons
9836436 Descente pour Cable tray fitting and accessories Tray Cable Drop Outs
Run Code Online (Sandbox Code Playgroud)
以下是我遵循的步骤:
训练
dataset=pd.read_csv("trainset.csv",encoding = "ISO-8859-1",low_memory=False)
dataset['description']=dataset['description'].str.replace('[^a-zA-Z]', ' ')
dataset['description']=dataset['description'].str.replace('[\d]', ' ')
dataset['description']=dataset['description'].str.lower()
stop = stopwords.words('english')
lemmatizer = WordNetLemmatizer()
dataset['description']=dataset['description'].str.replace(r'\b(' + r'|'.join(stop) + r')\b\s*', ' ')
dataset['description']=dataset['description'].str.replace('\s\s+',' ')
dataset['description'] =dataset['description'].apply(word_tokenize)
ADJ, ADJ_SAT, ADV, NOUN, VERB = 'a', 's', 'r', …Run Code Online (Sandbox Code Playgroud)我有一个熊猫数据框
id text
1 acclrtr actn corr cr
2 plate corr aff
3 alrm alt
Run Code Online (Sandbox Code Playgroud)
和字典
dict={'acclrtr':'accelerator','actn':'action','corr':'corrosion','cr':'chemical resistant','aff':'affinity','alrm':'alarm','alt':'alternate'}
Run Code Online (Sandbox Code Playgroud)
我需要用其值替换在数据框中找到的字典键
我尝试了以下代码,但没有一个能正常工作
1。
data['text']=data['text'].str.replace(dict.keys(), dict.values())
Run Code Online (Sandbox Code Playgroud)
2。
data['text']=data['text'].replace(dict, inplace=True)
Run Code Online (Sandbox Code Playgroud)
3。
data['text']=data['text'].apply(lambda x: [item.replace(to_replace=dict) for item in x])
Run Code Online (Sandbox Code Playgroud)
4。
for key, value in dict.items():
data['text']=data['text'].apply(lambda x: list(set([item.replace(key,value) for item in x])))
Run Code Online (Sandbox Code Playgroud)
谁能告诉我,我在哪里做错了,以及如何正确地用值替换密钥?
我有一个字符串
c=("Snap-on Power M1302A5 Imperial,IMPRL 0.062IN")
Run Code Online (Sandbox Code Playgroud)
我需要将上面的字符串转换为
c=("Snap-on Power Imperial,IMPRL")
Run Code Online (Sandbox Code Playgroud)
即我需要删除同时包含字母和数字的字符串,
我怎么能在python中做到这一点?
我试过
c=c.apply(word_tokenize)
c = c.apply(lambda x: [item for item in x if item.isalpha()])
Run Code Online (Sandbox Code Playgroud)
但得到了输出
c=("Snap-on Power MA Imperial,IMPRL IN")
Run Code Online (Sandbox Code Playgroud) pandas ×2
python ×2
dataframe ×1
dictionary ×1
isalpha ×1
large-data ×1
preprocessor ×1
python-3.x ×1
replace ×1
scikit-learn ×1