我怎样才能实现类似 np.where(df[variable] in ['value1','value2'])
how could I achieve something like np.where(df[varaible] in ['value1','value2'])
您好,我想在 ['value1','value2']
等条件下将一个分类变量的值更改为 other
这是我的代码:
random_sample['NAME_INCOME_TYPE_ind'] = np.where(random_sample['NAME_INCOME_TYPE'] in ['Maternity leave', 'Student']), 'Other')
我尝试在这行代码的不同位置添加.any()
,但仍然没有解决错误。
ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
您可以使用str.contains
来检查满足条件的地方:
l = ('|').join(['Maternity leave', 'Student'])
m = random_sample['NAME_INCOME_TYPE'].str.contains(l)
您还可以使用 .isin
:
生成 m
random_sample['NAME_INCOME_TYPE'].isin(['Maternity leave', 'Student'])
然后使用np.where
。但是请注意,您不能只指定两个值中的一个来根据条件进行选择,您必须同时指定 x
和 y
。对于您的情况,您可以使用 df['NAME_INCOME_TYPE']
和 other
作为 x
和 y
:
random_sample['NAME_INCOME_TYPE_ind'] = np.where(m,
'Other',
random_sample['NAME_INCOME_TYPE'])
在示例数据帧上进行测试:
df = pd.DataFrame({'NAME_INCOME_TYPE':['word1','word2','Student']})
l = ('|').join(['Maternity leave', 'Student'])
m = random_sample['NAME_INCOME_TYPE'].str.contains(l)
df['NAME_INCOME_TYPE_ind'] = np.where(m, 'Other', df['NAME_INCOME_TYPE'])
NAME_INCOME_TYPE NAME_INCOME_TYPE_ind
0 word1 word1
1 word2 word2
2 Student Other
对分类变量使用Categorical Data
在处理分类时,您可以将类别 替换为另一个而不是替换字符串。这具有内存和性能优势,因为内部 Pandas 对分类数据使用因式分解。
df = pd.DataFrame({'NAME_INCOME_TYPE': ['Employed', 'Maternity leave',
'Benefits', 'Student']})
# turn object series to categorical
label_col = 'NAME_INCOME_TYPE'
df[label_col] = df[label_col].astype('category')
# define others
others = ['Maternity leave', 'Student']
others_label = 'Other'
# add new category and replace existing categories
df[label_col] = df[label_col].cat.add_categories([others_label])
df[label_col] = df[label_col].replace(others, others_label)
print(df)
NAME_INCOME_TYPE
0 Employed
1 Other
2 Benefits
3 Other
您还可以使用方法链接更简洁地编写此代码:
# define others
others, others_label = ['Maternity leave', 'Student'], 'Other'
# turn to categorical, add category, then replace
df['NAME_INCOME_TYPE'] = df['NAME_INCOME_TYPE'].astype('category')\
.cat.add_categories([others_label])\
.replace(others, others_label)
您好,我想在 ['value1','value2']
other
这是我的代码:
random_sample['NAME_INCOME_TYPE_ind'] = np.where(random_sample['NAME_INCOME_TYPE'] in ['Maternity leave', 'Student']), 'Other')
我尝试在这行代码的不同位置添加.any()
,但仍然没有解决错误。
ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
您可以使用str.contains
来检查满足条件的地方:
l = ('|').join(['Maternity leave', 'Student'])
m = random_sample['NAME_INCOME_TYPE'].str.contains(l)
您还可以使用 .isin
:
m
random_sample['NAME_INCOME_TYPE'].isin(['Maternity leave', 'Student'])
然后使用np.where
。但是请注意,您不能只指定两个值中的一个来根据条件进行选择,您必须同时指定 x
和 y
。对于您的情况,您可以使用 df['NAME_INCOME_TYPE']
和 other
作为 x
和 y
:
random_sample['NAME_INCOME_TYPE_ind'] = np.where(m,
'Other',
random_sample['NAME_INCOME_TYPE'])
在示例数据帧上进行测试:
df = pd.DataFrame({'NAME_INCOME_TYPE':['word1','word2','Student']})
l = ('|').join(['Maternity leave', 'Student'])
m = random_sample['NAME_INCOME_TYPE'].str.contains(l)
df['NAME_INCOME_TYPE_ind'] = np.where(m, 'Other', df['NAME_INCOME_TYPE'])
NAME_INCOME_TYPE NAME_INCOME_TYPE_ind
0 word1 word1
1 word2 word2
2 Student Other
对分类变量使用Categorical Data
在处理分类时,您可以将类别 替换为另一个而不是替换字符串。这具有内存和性能优势,因为内部 Pandas 对分类数据使用因式分解。
df = pd.DataFrame({'NAME_INCOME_TYPE': ['Employed', 'Maternity leave',
'Benefits', 'Student']})
# turn object series to categorical
label_col = 'NAME_INCOME_TYPE'
df[label_col] = df[label_col].astype('category')
# define others
others = ['Maternity leave', 'Student']
others_label = 'Other'
# add new category and replace existing categories
df[label_col] = df[label_col].cat.add_categories([others_label])
df[label_col] = df[label_col].replace(others, others_label)
print(df)
NAME_INCOME_TYPE
0 Employed
1 Other
2 Benefits
3 Other
您还可以使用方法链接更简洁地编写此代码:
# define others
others, others_label = ['Maternity leave', 'Student'], 'Other'
# turn to categorical, add category, then replace
df['NAME_INCOME_TYPE'] = df['NAME_INCOME_TYPE'].astype('category')\
.cat.add_categories([others_label])\
.replace(others, others_label)