【问题标题】:How to map key to multiple values to dataframe column?如何将键映射到多个值到数据框列?
【发布时间】:2019-03-16 14:43:45
【问题描述】:

我有一个如下所示的 df 列:

col1
Non Profit
Other-501c3
501c3
Sole Proprietor

如何创建一个字典对象或映射层(对所有建议开放),如果它符合条件并更改键值,我可以在其中传递任何值?

例如,如果值为Other-501c3,则将其更改为non-profit

示例(等号后的所有内容都需要更改为等号前的值):

1. non-profit = (Non Profit, Other-501c3, 501c3,NON-Profit, Not-for-profit).

2. Sole Proprietor = (Sole Proprietor,Sole Proprietorship)

解决方案应该是可扩展的,我可以添加更多的“键值”对

提前谢谢你。

【问题讨论】:

    标签: python python-3.x pandas dictionary series


    【解决方案1】:

    keys 创建字典,合并它们和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
    

    【讨论】:

      【解决方案2】:

      @jezrael's 类似的解决方案,但您可以使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-12
        • 2019-03-21
        • 2016-06-04
        • 2017-07-24
        • 2020-08-26
        • 2021-09-11
        • 1970-01-01
        相关资源
        最近更新 更多