通过字典替换特定Dataframe列中的值
📁 python
需要通过字典替换特定Dataframe列中的值,如果未找到,则需要从Dataframe中删除整行。
我能够完成任务如下:
dict1 = {'car': 'c', 'motorcycle': 'mt', 'truck': 't'} df = pd.DataFrame({'product': 'truck motorcycle car tire wheel truck'.split(), 'price': [10000, 5000, 23000, 15000, 500, 300, 8000]}) df['product_new'] = df['product'].map(dict1)