Search code examples
pythonpython-3.xpandasdataframepandas-groupby

How to map key to multiple values to dataframe column w/ multiple values?


I ultimately want to group by items that have multiple shipping requirements vs ones that have just 1.

I have a pandas df column that looks like this:

ID #(column name = ID)
1111
1111,2222
1111,2222
2222,4444,3333
2222,4444

How can I create a dictionary object or mapping layer(open to all suggestions) where I can pass any value if it matches criteria and changes to the key value?

For example if the value is 1111, 4444 then change it to Express Shipping, Standard Shipping and have that be in the same dataframe.

I ultimately want to group by items that have multiple shipping requirements vs ones that have just 1.

1. shipping_num = (1111, 2222, 3333, 4444).

2. shipping_map = (Express shipping, Standard Shipping, 2-day shipping, 1-day shipping)



*NEW_SHIPPING MAP COLUMN*
Express shipping
Express shipping, Standard Shipping
Standard Shipping, 1-day shipping,2-day shipping
Standard Shipping, 1-day shipping

Thanks for looking!


Solution

  • You could create a mapping dictionary from shipping_num and shipping_map, then use str.split + explode to get individual ID numbers from the ID column. Then use map to get shipping maps; finally use groupby + agg to get back to original shape:

    shipping_num = (1111, 2222, 3333, 4444)
    shipping_map = ('Express shipping','Standard Shipping', '2-day shipping', '1-day shipping')
    
    mapping = dict(zip(shipping_num, shipping_map))
    df['shipping_map'] = df['ID'].str.split(',').explode().astype(int).map(mapping).groupby(level=0).agg(', '.join)
    

    Output:

                   ID                                       shipping_map
    0            1111                                   Express shipping
    1       1111,2222                Express shipping, Standard Shipping
    2       1111,2222                Express shipping, Standard Shipping
    3  2222,4444,3333  Standard Shipping, 1-day shipping, 2-day shipping
    4       2222,4444                  Standard Shipping, 1-day shipping