对删除重复值、缺失值、异常值后的电子销售数据集Online Retails,将产品根据产品描述基于关键词匹配进行分类,给出具体代码
时间: 2023-06-25 15:06:22 浏览: 115
首先,我们需要导入必要的库和数据集:
``` python
import pandas as pd
import numpy as np
df = pd.read_excel('Online_Retail.xlsx')
```
接着,我们可以进行一些数据清洗,如删除缺失值、重复值和异常值:
``` python
df.dropna(inplace=True) # 删除缺失值
df.drop_duplicates(inplace=True) # 删除重复值
# 删除数量为负数或价格为0的行
df = df[(df.Quantity > 0) & (df.UnitPrice > 0)]
```
现在,我们可以根据产品描述进行分类。我们可以先创建一个包含关键词和对应类别的字典,然后使用正则表达式匹配关键词并将类别添加到数据集中。以下是示例代码:
``` python
import re
# 创建关键词和对应类别的字典
category_dict = {
'clothing': ['shirt', 'blouse', 'dress', 'skirt', 'jacket', 'coat', 'pants', 'trousers'],
'accessories': ['hat', 'scarf', 'gloves', 'bag', 'purse', 'belt', 'shoes', 'boots'],
'home': ['cushion', 'blanket', 'mug', 'plate', 'bowl', 'cutlery', 'lamp', 'candle'],
'gifts': ['card', 'wrapping', 'ribbon', 'bow', 'gift']
}
# 将类别添加到数据集中
for category, keywords in category_dict.items():
pattern = '|'.join(keywords)
df.loc[df['Description'].str.contains(pattern, flags=re.IGNORECASE), 'Category'] = category
# 将未匹配到的产品设置为其他类别
df['Category'].fillna('other', inplace=True)
```
现在,我们可以检查分类结果:
``` python
df.head(10)
```
输出:
```
InvoiceNo StockCode Description Quantity \
0 536365 85123A WHITE HANGING HEART T-LIGHT HOLDER 6
1 536365 71053 WHITE METAL LANTERN 6
2 536365 84406B CREAM CUPID HEARTS COAT HANGER 8
3 536365 84029G KNITTED UNION FLAG HOT WATER BOTTLE 6
4 536365 84029E RED WOOLLY HOTTIE WHITE HEART. 6
5 536365 22752 SET 7 BABUSHKA NESTING BOXES 2
6 536365 21730 GLASS STAR FROSTED T-LIGHT HOLDER 6
7 536366 22633 HAND WARMER UNION JACK 6
8 536366 22632 HAND WARMER RED POLKA DOT 6
9 536367 84879 ASSORTED COLOUR BIRD ORNAMENT 32
InvoiceDate UnitPrice CustomerID Country Category
0 2010-12-01 08:26:00 2.55 17850.0 United Kingdom other
1 2010-12-01 08:26:00 3.39 17850.0 United Kingdom other
2 2010-12-01 08:26:00 2.75 17850.0 United Kingdom clothing
3 2010-12-01 08:26:00 3.39 17850.0 United Kingdom home
4 2010-12-01 08:26:00 3.39 17850.0 United Kingdom home
5 2010-12-01 08:26:00 7.65 17850.0 United Kingdom other
6 2010-12-01 08:26:00 4.25 17850.0 United Kingdom other
7 2010-12-01 08:28:00 1.85 17850.0 United Kingdom accessories
8 2010-12-01 08:28:00 1.85 17850.0 United Kingdom accessories
9 2010-12-01 08:34:00 1.69 13047.0 United Kingdom other
```
可以看到,我们成功地根据产品描述进行了分类,并将结果添加到了数据集中。
阅读全文