# Dict specifying the types to replace
type_dict = {'fast-food':'food','sit-down-food':'food',
'tech-pub':'pub','pub-and-dine':'pub'}
# Replace types that are dict keys but keep the values that aren't dict keys
df['type'] = [type_dict.get(i,i) for i in df['type']]
可以使用由要替换为所需类型的类型键入的dict作为值。然后将列设置为列表,替换类型,但保留所需的类型
我的第一反应是使用pandas apply函数来映射所需的值。大致如下:
当我运行这个程序时,产生了以下输出
相关问题 更多 >
编程相关推荐