How to map key to multiple values to dataframe column?

后端 未结 2 1476
一个人的身影
一个人的身影 2021-01-03 04:06

I have a df column that looks like this:

col1
Non Profit
Other-501c3
501c3
Sole Proprietor

How can I create a dictionary object or mapping

相关标签:
2条回答
  • 2021-01-03 04:44

    Similar solution to @jezrael's, but instead of creating a new dictionary you can use collections.ChainMap:

    from collections import ChainMap
    
    # dataframe setup
    df = pd.DataFrame({'col1': ['Non Profit', 'Other-501c3', '501c3', 'Sole Proprietor']})
    
    # create ChainMap
    L1 = ['Non Profit', 'Other-501c3', '501c3','NON-Profit', 'Not-for-profit']
    L2 = ['Sole Proprietor','Sole Proprietorship']
    d = ChainMap(dict.fromkeys(L1, 'non-profit'), dict.fromkeys(L2, 'Sole Proprietor'))
    
    # map values
    df['new'] = df['col1'].map(d.get)
    
    print(df)
    
                  col1              new
    0       Non Profit       non-profit
    1      Other-501c3       non-profit
    2            501c3       non-profit
    3  Sole Proprietor  Sole Proprietor
    
    0 讨论(0)
  • 2021-01-03 04:51

    Create dictionaries from keys, merge them and map:

    L1 = ['Non Profit', 'Other-501c3', '501c3','NON-Profit', 'Not-for-profit']
    d1 = dict.fromkeys(L1, 'non-profit')
    
    L2 = ['Sole Proprietor','Sole Proprietorship']
    d2 = dict.fromkeys(L2, 'Sole Proprietor')
    
    d = {**d1, **d2}
    print (d)
    {'Non Profit': 'non-profit', 
     'Other-501c3': 'non-profit', 
     '501c3': 'non-profit',
     'NON-Profit': 'non-profit', 
     'Not-for-profit': 'non-profit', 
     'Sole Proprietor': 'Sole Proprietor',
     'Sole Proprietorship': 'Sole Proprietor'}
    
    df['new'] = df['col1'].map(d)
    print (df)
                  col1              new
    0       Non Profit       non-profit
    1      Other-501c3       non-profit
    2            501c3       non-profit
    3  Sole Proprietor  Sole Proprietor
    
    0 讨论(0)
提交回复
热议问题