Mapping Python dictionary with multiple keys into dataframe with multiple columns matching keys

后端 未结 2 1660
南方客
南方客 2021-01-01 03:22

I have a dictionary that I would like to map onto a current dataframe and create a new column. I have keys in a tuple, which map onto two different columns in my dataframe.<

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

    You can create a MultiIndex from two series and then map. Data from @ALollz.

    df['CountyType'] = df.set_index(['County', 'State']).index.map(dct.get)
    
    print(df)
    
      County  State CountyType
    0      A      1        One
    1      A      2       None
    2      B      1       None
    3      B      2        Two
    4      B      3      Three
    
    0 讨论(0)
  • 2021-01-01 04:17

    If you have the following dictionary with tuples as keys and a DataFrame with columns corresponding to the tuple values

    import pandas as pd
    dct = {('A', 1): 'One', ('B', 2): 'Two', ('B', 3): 'Three'}
    df = pd.DataFrame({'County': ['A', 'A', 'B', 'B', 'B'],
                       'State': [1, 2, 1, 2, 3]})
    

    You can create a Series of the tuples from your df and then just use .map()

    df['CountyType'] = pd.Series(list(zip(df.County, df.State))).map(dct)
    

    Results in

      County  State CountyType
    0      A      1        One
    1      A      2        NaN
    2      B      1        NaN
    3      B      2        Two
    4      B      3      Three
    
    0 讨论(0)
提交回复
热议问题