Map dataframe index using dictionary

后端 未结 6 1866
Happy的楠姐
Happy的楠姐 2020-12-08 20:29

Why doesn\'t df.index.map(dict) work like df[\'column_name\'].map(dict)?

Here\'s a little example of trying to use index.map:



        
6条回答
  •  佛祖请我去吃肉
    2020-12-08 20:55

    As of pandas version 0.23.x (released at May 15th, 2018) this problem is fixed:

    import pandas as pd
    pd.__version__        # 0.23.4
    
    df = pd.DataFrame({'one': {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}})
    map_dict = {'A': 'every', 'B': 'good', 'C': 'boy', 'D': 'does', 'E': 'fine'}
    df
    #    one
    # A   10
    # B   20
    # C   30
    # D   40
    # E   50
    df.index.map(map_dict)
    #        one
    # every   10
    # good    20
    # boy     30
    # does    40
    # fine    50
    

    From the What's New page for pandas 0.23.0 it says:

    Index.map() can now accept Series and dictionary input objects (GH12756, GH18482, GH18509).

    For more information, check the help page of Index.map

提交回复
热议问题