I have a dataframe, df, in pandas with a column called 'Primary Sector - Org'. I also have a dictionary called dictionary. I want to replace elements of that column that are the values in my dictionary with the keys of the dictionary.
For example my dictionary looks like this:
dictionary = {
"transport" : ['Transport', 'Transportation'],
"buildings" : ['Housing','Provision of Housing'],
"energy" : ["Energy", "Energy; Housing; Income or financial inclusion"],
"food" : ["Food catering or production (incl. farming)"],
"waste and consumption" : ["Waste Reduction Re-use or Recycling", "Waste Reduction, Reuse or Recycling"],
"natural envrionment" : ["Environmnet or nature", "Environmnet or Nature","Environmnet or Nature; Other", "Environmnet or nature conservation","Environmnet or nature conservation, Employmnet support or training",
"Environment or Nature; Health Care or Wellbeing", "Horticulture"]
}
When the value of say "Environmnet or nature" appreas in the column, I want to replace it with "natural environment". How would I do this? I have have tried doing the following:
df['Primary Sector - Org'] = df.apply(lambda x: [dictionary[i] if i in dictionary else '' for i in x],axis=1)
df
But that does not replace the elements correctly. Thanks so much if you could help :)