2 回答
TA贡献1784条经验 获得超2个赞
我假设您的意思是您想要一个数据框,其中“类别”中的值是“家具”或“技术”。这是你可以做的事情。
df[df['Category'].isin(['Furniture ', 'Technology '])]
如果这不是你的意思,也许你可以澄清一下。
编辑:在下面回复您的评论:
df['Category_filter'] = df['Category'].where(df['Category'].isin(['Furniture ', 'Technology ']))
TA贡献1812条经验 获得超5个赞
如果我正确理解了您,那么您正在查找列中每个元素重复的值的总数。
示例数据帧:
>>> df
Category
0 Furniture
1 Technology
2 Office Supply
3 Furniture
4 Technology
5 Office Supply
6 Furniture
7 Technology
8 Office Supply
根据您更新的代码它应该工作,只有您不匹配的值才会报告为NaN..
>>> df['Category_Filter'] = df[df['Category'].isin(['Furniture', 'Technology'])]
>>> df
Category Category_Filter
0 Furniture Furniture
1 Technology Technology
2 Office Supply NaN
3 Furniture Furniture
4 Technology Technology
5 Office Supply NaN
6 Furniture Furniture
7 Technology Technology
8 Office Supply NaN
或者,如果您希望删除所有具有NaN值的行,只需尝试:
>>> df.dropna()
# df.dropna(inplace=True) # make in permanent to the DataFrame
Category Category_Filter
0 Furniture Furniture
1 Technology Technology
3 Furniture Furniture
4 Technology Technology
6 Furniture Furniture
7 Technology Technology
添加回答
举报