Pandas Numpy np.where空值的处理

2024-05-21 13:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python脚本中有一个np.where语句,它根据另一列中的搜索分配新列值,如下所示:

df['campaignNameFilter'] = pd.np.where(df['campaignLabels'].str.contains("Core Brand", case=False), "Core Brand",
pd.np.where(df['campaignLabels'].str.contains("Product Ranges", case=False), "Product Ranges",
pd.np.where(df['campaignLabels'].str.contains("Product Types", case=False), "Product Types",
pd.np.where(df['campaignLabels'].str.contains("Other", case=False), "Other",
pd.np.where(df['campaignLabels'].str.contains("Sports", case=False), "Sports",
pd.np.where(df['campaignLabels'].str.contains("Brand Expanded", case=False), "Brand Expanded",
pd.np.where(df['campaignLabels'].str.contains("Non-Brand", case=False), "Non-Brand",
pd.np.where(df['campaignLabels'].str.contains("Brand", case=False), "Brand",
pd.np.where(df['campaignLabels'].str.contains("Sub-Brand", case=False), "Sub-Brand",
pd.np.where(df['campaignLabels'].str.contains("Product ID", case=False), "Product ID",
pd.np.where(df['campaignLabels'].str.contains("All Products", case=False), "All Products",
pd.np.where(df['campaignLabels'].str.contains("Showcase", case=False), "Showcase", "NOT ASSIGNED"))))))))))))

因此,它将遍历“活动标签”列,并将值分配给名为“活动名称过滤器”的新列。现在,如果未找到任何匹配项,“活动名称筛选器”将被指定为未分配

这一切都很好,但是,问题是当活动标签列为空时,它会自动将其指定为第一个语句,因此在这种情况下,它将是“核心品牌”。这是Numpy/Pandas中的错误还是预期的行为

我使用的是Pandas 0.24.2和Numpy 1.16.3。 非常感谢您的帮助


Tags: corefalsedfnp语句productwheretypes
1条回答
网友
1楼 · 发布于 2024-05-21 13:25:21

我建议在处理多个选项时使用numpy.select(),而不是使用numpy.where(),如果没有匹配,则使用numpy.select()。您需要设置满足的条件和结果,以便:

import numpy as np

conditions = [df['campaignLabels'].str.contains("Core Brand", case=False),
             df['campaignLabels'].str.contains("Product Ranges", case=False),
             df['campaignLabels'].str.contains("Product Types", case=False),
             df['campaignLabels'].str.contains("Other", case=False),
             # ... you get the idea
             ]

results = ["Core Brand",
           "Product Ranges",
           "Product Types",
           "Other",
           # ... you get the idea
          ]

else_result = "NOT ASSIGNED"

df['campaignNameFilter'] = np.select(conditions, results, else_result)

相关问题 更多 >