簡體   English   中英

如何更新另一列值與特定值匹配的一列行的值?

[英]How to update the values of one column's row where another column value matches specific value?

我有一個包含四列id1id2config_typecall_frequency ,但是, id1id2無關緊要。

我需要用條件與另一列匹配的特定字符串替換call_frequency列的值。

輸入:

在此處輸入圖片說明

輸出:

![在此處輸入圖片描述

基本上,當config_types匹配時,我需要替換相應的call_frequency列中的值。

{'type2':'string2', 'type3':'string3', 'type4':'string4'}

並且不匹配的值應保持不變。

我試過了:

df[df.config_type == 'dict_key', 'column'] = 'dict_value'

但這給了我錯誤。

TypeError:“系列”對象是可變的,因此無法進行哈希處理

任何想法如何解決這個問題?

使用numpy.where替代方法:

import numpy as np

d = {'type2':'string2', 'type3':'string3', 'type4':'string4'}
df["call_frequency"]=np.where(df['config_type'].isin(d), df['config_type'].replace(d), df['call_frequency'])
  • 使用loc
d = {'type2':'string2', 'type3':'string3', 'type4':'string4'}
for k,v in d.items():
    df.loc[df.config_type==k, 'call_frequency'] = v

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM