[英]How to update the values of one column's row where another column value matches specific value?
我有一個包含四列id1
, id2
, config_type
, call_frequency
,但是, id1
和id2
無關緊要。
我需要用條件與另一列匹配的特定字符串替換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.