2017-04-11 6 views
4

Warum funktioniert df.index.map (dict) nicht wie df ['column_name']. Map (dict)?Map-Dataframe-Index mit Wörterbuch

Hier ist ein kleines Beispiel zu versuchen, verwenden index.map:

import pandas as pd 

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['two'] = df.index.map(mapper=map_dict) 

Dies wirft TypeError: 'dict' object is not callable

es eine Lambda-Fütterung funktioniert:

df['two'] = df.index.map(mapper=(lambda x: map_dict[x])); df 
''' 
    one two 
A 10 every 
B 20 good 
C 30 boy 
D 40 does 
E 50 fine 
''' 

jedoch den Index und Mapping Zurücksetzen auf einer Säule funktioniert wie erwartet ohne Beanstandung:

df.reset_index(inplace=True) 
df.rename(columns={'index': 'old_ndx'}, inplace=True) #so there's no index name confusion 
df['two'] = df.old_ndx.map(map_dict); df 

''' 
    old_ndx one two 
0  A 10 every 
1  B 20 good 
2  C 30 boy 
3  D 40 does 
4  E 50 fine 
''' 
+2

Nach [die Dokumentation] (http: //pandas.pydata .org/pandas-docs/version/0.18.1/generierte/pandas.Index.map.html), erfordert 'pandas.Index.map' eine aufrufbare Datei. Ist Ihre Frage * warum * wurde diese Designentscheidung getroffen? –

+2

[Hier] (https://github.com/pandas-dev/pandas/issues/12756) ist ein relevantes Problem. Es scheint, dass es nur etwas ist, das durch die Risse gerutscht ist, dass sie nicht zum Reparieren gekommen sind. Es scheint, dass es [wird derzeit behoben] (https://github.com/pandas-dev/pandas/pull/15081). –

Antwort

5

Ich bin nicht Ihre Frage zu beantworten ... Nur Sie eine bessere Arbeit um zu geben.
Verwenden to_series() sie map

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['two'] = df.index.to_series().map(map_dict) 

df 

    one two 
A 10 every 
B 20 good 
C 30 boy 
D 40 does 
E 50 fine 
0

map (a Python Schlüsselwort) wird offensichtlich als eine Methode der df.index

verwendet werden, weil dies seine eigene interne Anforderungen hat, ist es ein Argument übergeben, der keine __call__ Verfahren hat nicht erlaubt.

lambda und Funktionen sind aufrufbar, ein einfacher Test:

def foo(): 
    pass 
if foo.__call__: 
    print True 
# Prints True 

bar = lambda x: x+1 
if bar.__call__: 
    print True 
# Prints True 

print {'1':'one'}.__call__ 
# AttributeError: 'dict' object has no attribute '__call__' 
+1

'map' wird nicht" überschrieben ". 'map' ist eine Funktion, keine Methode, also gibt es nichts zu überschreiben. –

5

Eine alternative Lösung zu fordern Karte:

df['two'] = pd.Series(map_dict) 

df 

    one two 
A 10 every 
B 20 good 
C 30 boy 
D 40 does 
E 50 fine 

Auf jeden Fall, bis das Zuordnungsproblem (pro juanpa.arrivillaga Kommentar) aufgelöst wird, müssen Sie konvertieren entweder die index oder das dict-to-map zu einer pandas-serie.

0

Eine kürzere alternative --with keine expliziten Aufruf an to_series oder pd.Series:

df['two'] = df.rename(map_dict).index 
3

get am Ende Hinzufügen

df['Two']=df.index.map(map_dict.get) 
df 
Out[155]: 
    one Two 
A 10 every 
B 20 good 
C 30 boy 
D 40 does 
E 50 fine