我在这个问题上遇到了点麻烦。我有一个服装项目的数据框,其相关描述如下:
Item Description
R2G1 RED, BLUE, SHIRT
G23A YELLOW SHIRT
P001 BLUE, PINK SKIRT
我也有一个项目清单,其中包含可能的服装项目类别,即
categories = ['RED', 'BLUE', 'YELLOW', 'PINK', 'SHIRT', 'SKIRT']
我需要检查每个项目的描述字段,看看它们是否包含类别列表中的任何字符串,并根据类别在新列中为它们分配正确的二进制值。最终输出应如下所示:
Item Description Red Blue Yellow Pink Shirt Skirt
R2G1 RED, BLUE, SHIRT 1 1 0 0 1 0
G23A YELLOW SHIRT 0 0 1 0 1 0
P001 BLUE, PINK SKIRT 0 1 0 1 0 1
我曾尝试使用此函数,但我一直得到AttributeError:“float”对象没有属性“upper”错误,当我尝试使用它时,如下所示:
def get_category(series):
res = []
for i in category_list:
if i in series.upper():
res.append(i)
return res
df['Categories'] = df['Description'].apply(get_model)
df = df.join(df['Model'].str.join('|').str.get_dummies())
如果使项具有统一的分隔符,则可以使用^{} 的
sep
参数来获取一个热编码。为此,我将空格替换为逗号:那么你只需要加入:
但是需要注意的是(正如Rob所评论的),这是从
Description
列确定类别,而不是从categories
列表本身确定类别。因此,如果您有不在categories
中的描述,您将有额外的列。例如,如果一个描述包含"GREEN"
,您将得到整个绿色列同样,描述中不存在的类别也不会作为列包含。因此,如果说第一行丢失了,那么RED就没有列了
如果这是一个问题,我可以想办法修复这些行为,但我认为更简单的方法是使用heretolearn's answer或其他明确包含
categories
的方法您可以尝试以下方法:
输出:
使用
findall
和MultiLabelBinarizer
的另一种方法在
findall
之后series.str.get_dummies
的更慢但更简单的版本只有在加入它们之后:相关问题 更多 >
编程相关推荐