【发布时间】:2016-12-02 00:38:12
【问题描述】:
我正在尝试为大学名称列表分配州名:
df = pd.DataFrame({'College': pd.Series(['University of Michigan', 'University of Florida', 'Iowa State'])})
State = ['Michigan', 'Iowa']
df['State'] = np.where(df['College'].str.contains('|'.join(State)),
'state','--')
我想将匹配时出现的“州”值替换为州的实际名称。示例:密歇根大学 -> 密歇根大学(而不是“州”)。最终,“状态”将包含所有 50 个状态,因此我不能为每个状态名称编写 50 个“np.where”语句。
感谢您的帮助。
【问题讨论】:
标签: python pandas contains assign extraction