Map dataframe index using dictionary

后端 未结 6 1862
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:46

    An alternative workaround to calling map:

    df['two'] = pd.Series(map_dict)
    
    df
    
       one    two
    A   10  every
    B   20   good
    C   30    boy
    D   40   does
    E   50   fine
    

    In any case, until the mapping issue gets resolved (per juanpa.arrivillaga's comment) you have to convert either the index or the dict-to-map to a pandas Series.

提交回复
热议问题