【问题标题】:Equivalent of R/ifelse in Python/Pandas? Compare string columns?Python/Pandas 中的 R/ifelse 等价物?比较字符串列?
【发布时间】: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')

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    您必须将 pandas 升级到最新版本,因为在 0.17.1 版本中它运行良好。

    示例(install_month 列中的第一个值已更改以进行匹配):

    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']
    

    【讨论】:

      【解决方案2】:

      一种选择是将匿名函数Pandas的应用函数结合使用:

      在函数中设置一些分支逻辑:

      def if_this_else_that(x, list_of_checks, yes_label, no_label):
          if x in list_of_checks:
              res = yes_label
          else: 
              res = no_label
          return(res)
      

      这需要来自 lambda 的 x(见下文)、要查找的 列表yes 标签和 没有标签。

      例如,假设我们正在查看 IMDB 数据集 (imdb_df):

      ...我想添加一个名为“new_rating”的新列,用于显示电影是否成熟。

      我可以将 Pandas apply 函数与上面的分支逻辑一起使用:

      imdb_df['new_rating'] = imdb_df['Rated'].apply(lambda x: if_this_else_that(x, ['PG', 'PG-13'], 'not mature', 'mature'))
      

      有时我们需要将此与另一项检查结合起来。例如,IMDB 数据集中的一些条目是 NaN。我可以检查 NaN 和成熟度等级,如下所示:

      imdb_df['new_rating'] = imdb_df['Rated'].apply(lambda x: 'not provided' if x in ['nan'] else if_this_else_that(x, ['PG', 'PG-13'], 'not mature', 'mature'))
      

      在这种情况下,我的 NaN 首先被转换为字符串,但您显然也可以使用真正的 NaN 来执行此操作。

      【讨论】:

        猜你喜欢
        • 2010-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-23
        • 2016-06-25
        • 1970-01-01
        • 2018-07-11
        相关资源
        最近更新 更多