简体   繁体   English

如果使用 Python 满足特定条件,则替换列中的值

[英]Replace values within a column if a certain condition is met using Python

I have a dataset where I would like to map values based on a specific condition and override the values that are in an existing column.我有一个数据集,我想根据特定条件映射值并覆盖现有列中的值。

Data数据

ID  Date    Location    Used  Status
AA  Q121    NY          20    ok
AA  Q221    NY          50    ok
AA  Q321    NY          10    ok
BB  Q121    CA          1     ok
BB  Q221    CA          0     yes
BB  Q321    CA          500   yes
BB  Q421    CA          700   no
CC  Q121    AZ          50    no

Desired期望的

ID  Date    Location    Used    Status
AA  Q121    NY                  closed
AA  Q221    NY                  closed
AA  Q321    NY                  closed
BB  Q121    CA          1       ok
BB  Q221    CA          0       yes
BB  Q321    CA          500     yes
BB  Q421    CA          700     no
CC  Q121    AZ          50      no

Doing正在做

df['Used'] = np.where(((df.ID == 'AA') & (df.Date >= 'Q121')), '', df['Used'])
df['Status'] = np.where(((df.ID == 'AA') & (df.Date >= 'Q121')), 'closed', '')

The script above replaces 'ok' with 'closed' , however, it also deletes the remaining values in the column.上面的脚本将 'ok' 替换为 'closed' ,但是,它也会删除列中的剩余值。 Any suggestion is appreciated.任何建议表示赞赏。

This should work -这应该工作 -

df.loc[(df.ID == 'AA') & (df.Date >= 'Q121'), 'Used'] = ''
df.loc[(df.ID == 'AA') & (df.Date >= 'Q121'), 'Status'] = 'closed'

Please see the answer using np.where as stated in the question:请使用问题中所述的 np.where 查看答案:

d = {
    'ID': ['AA', 'AA', 'AA', 'BB', 'BB'],
    'Date': ['Q121', 'Q221', 'Q321', 'Q121', 'Q221'],
    'Location': ['NY', 'NY', 'NY', 'CA', 'CA'],
    'Used': [20, 50, 10, 1, 0],
    'Status': ['ok', 'ok', 'ok', 'ok', 'yes']
}

df = pd.DataFrame(d)

idx = np.where((df.ID == 'AA') & (df.Date >= 'Q121'))[0].tolist()

df.loc[idx, 'Used'] = np.nan
df.loc[idx, 'Status'] = 'Closed'

您可以使用申请:

df['Used'],df['Status'] = zip(*df.apply(lambda x: ('', 'closed') if (x.ID == 'AA') and (x.Date >= 'Q121') else (x.Used, x.Status), axis = 1))
df.loc[(df['ID'].eq('AA')) & (df['Date'] >= 'Q121'), 'Status'] = 'closed'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM