【问题标题】:changing the entries in one column based on the the values in other column [duplicate]根据另一列中的值更改一列中的条目[重复]
【发布时间】:2019-12-16 02:38:35
【问题描述】:

我的两列有 13961 行,基于一列中的值,我需要更改另一列中的值。

  1. 如果数据[CO BORROWER NAME'] 已标记为“NOT_AVAILABLE”,我的其他列数据['CO BORROWER_STATUS'] 应标记为条目“NOT_AVAILABLE”

  2. 如果 data[CO BORROWER NAME'] 不等于 'NOT_AVAILABLE' 并且如果存在任何其他值,则 data['CO BORROWER_STATUS'] 应标记为 'AVAILABLE'

我已经制作了 for 循环,以及相同的 if 条件并迭代了相同的条件。但执行需要超过 15 分钟。还有其他简单的方法吗?

for i in range(0,13961):

    if data['CO BORROWER NAME'][i] == 'NOT_AVAILABLE':

        data['CO BORROWER_STATUS'][i]='NOT_AVAILABLE'

    else:

        data['CO BORROWER_STATUS'][i]='AVAILABLE'

O/p expected:    
data['CO BORROWER_STATUS'] column should have either 'AVAILABLE'/'NOT 
AVAILABLE' based on the condition in column data['CO BORROWER NAME'] as 
mentioned earlier

【问题讨论】:

  • 那些答案并不完全相关,它似乎匹配,但在运行时的基础上,我正在寻找一些有效的代码。请从“重复”中取消标记

标签: python pandas


【解决方案1】:

请尝试

import numpy as np
data['CO BORROWER_STATUS'] = np.select([data['a'] == 'NOT_AVAILABLE'],['NOT_AVAILABLE'], ['AVAILABLE'])

【讨论】:

  • 为什么建议使用 numpy,因为他想要使用 pandas 的解决方案
  • 性能方面,这可能是最佳实践之一。在此处查看接受的答案stackoverflow.com/questions/57409229/…
  • data.loc[data['CO BORROWER NAME'].eq('NOT_AVAILABLE'), 'CO BORROWER_STATUS'] = 'NOT_AVAILABLE'; data.loc[data['CO BORROWER NAME'].ne('NOT_AVAILABLE'), 'CO BORROWER_STATUS'] = 'AVAILABLE' 只是熊猫。虽然效率不高
  • 这太棒了,Neofytos 先生,这很有效,只需几秒钟。谢谢!!
猜你喜欢
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多