【发布时间】:2016-06-10 12:39:55
【问题描述】:
我的目标是比较两列并添加结果列。 R 使用 ifelse 但我需要知道 pandas 的方式。
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
熊猫
>>> 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
我尝试了一些案例,但没有成功。字符串比较好像不行。
>>>np.where(maupayment['log_month'] == maupayment['install_month'], 'install', 'existing')
TypeError: 'str' object cannot be interpreted as an integer
你能帮帮我吗?
Pandas 和 numpy 版本。
>>> pd.version.version
'0.16.2'
>>> np.version.full_version
'1.9.2'
更新版本后,它工作了!
>>> np.where(maupayment['log_month'] == maupayment['install_month'], 'install', 'existing')
array(['existing', 'install', 'existing', ..., 'install', 'install',
'install'],
dtype='<U8')
【问题讨论】: