Search code examples
pythonpandasdataframedictionarykey

Using keys from a dict to look for the same "keys" in a pandas dataframe, then assign value from dict to dataframe empty column


I have a pandas dataframe with zipcodes. I also have a dictionary where keys = zipkode and values = regions

The dictionary

my_regions = {8361: 'Central region', 8381: 'Central region', 8462: 'North region', 8520: 'South region', 8530: 'Central region', 8541: 'South region'}

The dataframe has a col name

df["zipcode"]= [8462, 8361, 8381,8660,8530,8530]

I want to add a new col to the dataframe df with the dict values (region name), when the loop sees that zip code in dataframe is == to zipkode in dict.keys

I have tried this

my_regions_list = []

for keyname in my_regions:
    for zipcode in df.zipcode:
        if zipcode == my_regions.keys():
            my_regions_list.append(my_regions.values())
            # df["region"] = df.append(my_regions.values())
            df =df.insert(column="region", value = my_r)

The list is empty and this is not adding the new row to the existing dataframe...

I also tried to convert it to a dataframe but it makes no sense

df1 = pd.DataFrame(list(my_regions.items()),columns = ['ZIPCODE','REGIONNAME'])

Solution

  • You can use .map():

    df = pd.DataFrame({"zipcode": [8462, 8361, 8381, 8660, 8530, 8530]})
    
    my_regions = {
        8361: "Central region",
        8381: "Central region",
        8462: "North region",
        8520: "South region",
        8530: "Central region",
        8541: "South region",
    }
    
    
    df["name"] = df["zipcode"].map(my_regions)
    print(df)
    

    Prints:

       zipcode            name
    0     8462    North region
    1     8361  Central region
    2     8381  Central region
    3     8660             NaN
    4     8530  Central region
    5     8530  Central region