I've a column in first data-frame df1["ItemType"]
as below,
Dataframe1
ItemType1
redTomato
whitePotato
yellowPotato
greenCauliflower
yellowCauliflower
yelloSquash
redOnions
YellowOnions
WhiteOnions
yellowCabbage
GreenCabbage
I need to replace that based on a dictionary created from another data-frame.
Dataframe2
ItemType2 newType
whitePotato Potato
yellowPotato Potato
redTomato Tomato
yellowCabbage
GreenCabbage
yellowCauliflower yellowCauliflower
greenCauliflower greenCauliflower
YellowOnions Onions
WhiteOnions Onions
yelloSquash Squash
redOnions Onions
Notice that,
- In
dataframe2
some of theItemType
are same asItemType
indataframe1
. - Some
ItemType
in dataframe2 havenull
values like yellowCabbage. ItemType
in dataframe2 are out of order with respect toItemType
indataframe
I need to replace values in Dataframe1
ItemType
column if there is a match for value in the corresponding Dataframe2
ItemType
with newType
keeping above exceptions listed in bullet-points in mind.
If there is no match, then values needs to be as they are [ no change].
So far I got is.
import pandas as pd
#read second `csv-file`
df2 = pd.read_csv('mappings.csv',names = ["ItemType", "newType"])
#conver to dict
df2=df2.set_index('ItemType').T.to_dict('list')
Below given replace on match are not working. They are inserting NaN
values instead of actual. These are based on discussion here on SO.
df1.loc[df1['ItemType'].isin(df2['ItemType'])]=df2[['NewType']]
OR
df1['ItemType']=df2['ItemType'].map(df2)
Thanks in advance
EDIT
Two column headers in both data frames have different names. So dataframe1 column on is ItemType1 and first column in second data-frame is ItemType2. Missed that on first edit.