2016-02-27 11 views
8

Mein Ziel ist es, zwischen zwei Spalten zu vergleichen und die Ergebnisspalte hinzuzufügen. R benutzt ifelse, aber ich muss Pandas kennen.Äquivalent von R/ifelse in Python/Pandas? Zeichenfolge-Spalten vergleichen?

R

> head(mau.payment) 
    log_month user_id install_month payment 
1 2013-06  1  2013-04  0 
2 2013-06  2  2013-04  0 
3 2013-06  3  2013-04 14994 

> mau.payment$user.type <-ifelse(mau.payment$install_month == mau.payment$log_month, "install", "existing") 
> head(mau.payment) 
    log_month user_id install_month payment user.type 
1 2013-06  1  2013-04  0 existing 
2 2013-06  2  2013-04  0 existing 
3 2013-06  3  2013-04 14994 existing 
4 2013-06  4  2013-04  0 existing 
5 2013-06  6  2013-04  0 existing 
6 2013-06  7  2013-04  0 existing 

Pandas

>>> maupayment 
user_id log_month install_month 
1  2013-06 2013-04    0 
     2013-07 2013-04    0 
2  2013-06 2013-04    0 
3  2013-06 2013-04   14994 

habe ich versucht, einige Fälle aber hat nicht funktioniert. Es scheint, dass der String-Vergleich nicht funktioniert.

>>>np.where(maupayment['log_month'] == maupayment['install_month'], 'install', 'existing') 

TypeError: 'str' object cannot be interpreted as an integer 

Können Sie mir bitte helfen?


Pandas und numpy Version.

>>> pd.version.version 
'0.16.2' 
>>> np.version.full_version 
'1.9.2' 

Nach Update Versionen, es funktionierte!

>>> np.where(maupayment['log_month'] == maupayment['install_month'], 'install', 'existing') 
array(['existing', 'install', 'existing', ..., 'install', 'install', 
     'install'], 
     dtype='<U8') 

Antwort

8

Sie haben Pandas letzte Version zu aktualisieren, da in Version 0.17.1 es sehr gut funktioniert.

Probe (erster Wert in Spalte install_month zur Anpassung geändert wird):

print maupayment 
    log_month user_id install_month payment 
1 2013-06  1  2013-06  0 
2 2013-06  2  2013-04  0 
3 2013-06  3  2013-04 14994 

print np.where(maupayment['log_month'] == maupayment['install_month'], 'install', 'existing') 
['install' 'existing' 'existing']