2013-06-14 15 views
17

Esiste comunque la possibilità di utilizzare la funzione di mappatura o qualcosa di meglio per sostituire i valori in un intero dataframe?python panda che sostituiscono stringhe nel dataframe con i numeri

So solo come eseguire la mappatura su serie.

vorrei sostituire le stringhe nel 'Tesst' e la colonna 'set' con un numero es set = 1, prova = 2

Ecco un esempio del mio set di dati: (dati originale è molto grande)

ds_r 
    respondent brand engine country aware aware_2 aware_3 age tesst set 
0   a volvo  p  swe  1  0  1 23 set set 
1   b volvo None  swe  0  0  1 45 set set 
2   c bmw  p  us  0  0  1 56 test test 
3   d bmw  p  us  0  1  1 43 test test 
4   e bmw  d germany  1  0  1 34 set set 
5   f audi  d germany  1  0  1 59 set set 
6   g volvo  d  swe  1  0  0 65 test set 
7   h audi  d  swe  1  0  0 78 test set 
8   i volvo  d  us  1  1  1 32 set set 

risultato finale dovrebbe essere

ds_r 
    respondent brand engine country aware aware_2 aware_3 age tesst set 
0   a volvo  p  swe  1  0  1 23  1 1 
1   b volvo None  swe  0  0  1 45  1 1 
2   c bmw  p  us  0  0  1 56  2 2 
3   d bmw  p  us  0  1  1 43  2 2 
4   e bmw  d germany  1  0  1 34  1 1 
5   f audi  d germany  1  0  1 59  1 1 
6   g volvo  d  swe  1  0  0 65  2 1 
7   h audi  d  swe  1  0  0 78  2 1 
8   i volvo  d  us  1  1  1 32  1 1 

grato per consigli,

risposta

23

Che dire DataFrame.replace?

In [9]: mapping = {'set': 1, 'test': 2} 

In [10]: df.replace({'set': mapping, 'tesst': mapping}) 
Out[10]: 
    Unnamed: 0 respondent brand engine country aware aware_2 aware_3 age \ 
0   0   a volvo  p  swe  1  0  1 23 
1   1   b volvo None  swe  0  0  1 45 
2   2   c bmw  p  us  0  0  1 56 
3   3   d bmw  p  us  0  1  1 43 
4   4   e bmw  d germany  1  0  1 34 
5   5   f audi  d germany  1  0  1 59 
6   6   g volvo  d  swe  1  0  0 65 
7   7   h audi  d  swe  1  0  0 78 
8   8   i volvo  d  us  1  1  1 32 

    tesst set 
0  2 1 
1  1 2 
2  2 1 
3  1 2 
4  2 1 
5  1 2 
6  2 1 
7  1 2 
8  2 1 

Come @Jeff sottolineato nei commenti, nelle versioni panda < 0.11.1, virare manualmente .convert_objects() sull'estremità per convertire correttamente Tesst e impostato int64 colonne, nel caso che conta nelle operazioni successive.

+0

+1 chiaramente la migliore soluzione –

+1

nota che potresti voler fare un '' df.convert_objects() '' dopo la sostituzione per forzare i dtypes appropriati – Jeff

+0

Grazie, posto !! – jonas

4

È possibile utilizzare la funzione applymap dataframe per fare questo:

In [26]: df = DataFrame({"A": [1,2,3,4,5], "B": ['a','b','c','d','e'], 
         "C": ['b','a','c','c','d'], "D": ['a','c',7,9,2]}) 
In [27]: df 
Out[27]: 
    A B C D 
0 1 a b a 
1 2 b a c 
2 3 c c 7 
3 4 d c 9 
4 5 e d 2 

In [28]: mymap = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5} 

In [29]: df.applymap(lambda s: mymap.get(s) if s in mymap else s) 
Out[29]: 
    A B C D 
0 1 1 2 1 
1 2 2 1 3 
2 3 3 3 7 
3 4 4 3 9 
4 5 5 4 2 
+0

io che lavorano sul problema come questo e ho appena seguito il passaggi esatti menzionati nella risposta. Non sto ottenendo l'output. ** Codice: ** wc = pd.read_csv ('PATH', usecols = ['Workclass']) – SRS

+0

df = pd.DataFrame (wc) ** fine riga ** wcdict = {"?": 0, "Federal-gov": 1, "Local-gov": 2, "Never-worked": 3, "Private": 4, "Self-emp-inc": 5, "Self-emp-n-inc" : 6, "State-gov": 7, "Senza pagamento": 8} ** fine riga ** df.applymap (lambda s: wcdict.get (s) se s in wcdict else s) ** fine di linea ** print (df) – SRS

+0

'df.applymap (lambda s: mymap.get (s) if s in mymap else s)' non apporta modifiche in linea a df, quindi l'istruzione 'print df' non riflette i risultati della applymap. Devi fare un assaggio come 'df2 = df.applymap (lambda s: mymap.get (s) se s in mymap else s)'. 'print df2' ora rifletterà le modifiche. – bdiamante

7

So che questo è vecchio, ma aggiungendo per quelli che cercano come ero. Creare un dataframe in panda, df in questo codice

ip_addresses = df.source_ip.unique() 
ip_dict = dict(zip(ip_addresses, range(len(ip_addresses)))) 

che vi darà una mappa dizionario degli indirizzi IP senza dover scrivere fuori.

0

per convertire le stringhe come 'volvo', 'bmw' in numeri interi prima convertirlo in un dataframe poi passarlo a pandas.get_dummies()

df = DataFrame.from_csv("myFile.csv") 
    df_transform = pd.get_dummies(df) 
    print(df_transform) 
Problemi correlati