转载仅供个人学习参考,侵删。
转载来自此处
如何从字典中替换dataframe列中的特定值?
所以,我有一张如下的表格:
Col1 Col2
ABS 45
CDC 23
POP 15
现在,我有一本字典。所以对于匹配的关键部分,我只希望列中的值改变。对于与字典不匹配的其他字母,键应保持不变。你知道吗
最后一个表应该如下所示:
Col1 Col2
ADBS 45
LCDLC 23
PLOPL 15
方法1:
df = pd.DataFrame([['ABS', '45'], ['CDC', '23'], ['POP', '15']], columns=('Col1', 'Col2'))
aa = {'A':'AD','P':'PL','C':'LC'}
pat = "|".join(aa.keys())
df["Col1"].str.replace(pat, lambda x: aa.get(x[0], x[0]))
方法2:
df = pd.DataFrame({'Col1': ['ABS', 'CDC', 'POP'],
'Col2': [45, 23, 15],
})
keys = aa.keys()
df.Col1 = [''.join([aa.get(e) if (e in keys) else e for e in list(ee)]) for ee in df.Col1.tolist()]
df
让我们用一种更易读的形式写下清单。我们创建一个函数do_something来理解列表理解的第一部分中发生了什么。第二部分(for ee in df.Col1.tolist())基本上迭代数据帧df的列’Col1’中的每一行。你知道吗
def do_something(x):
# here x is like 'ABS'
xx = '.join([aa.get(e) if (e in keys) else e for e in list(x)])
return xx
df.Col1 = [do_something(ee) for ee in df.Col1.tolist()]
拆包do_something(x)
函数do_something(x)执行以下操作。如果你用x = ‘ABS’试试,会更容易。do_something中的’'.join(some_list)加入生成的列表。下面的代码块将说明这一点。你知道吗
x = 'ABS'
print(do_something(x))
[aa.get(e) if (e in keys) else e for e in list(x)]
输出
ADBS
['AD', 'B', 'S']
那么核心逻辑是什么呢?
下面的代码块逐步向您展示了逻辑是如何工作的。显然,在解决方案开头引入的列表推导会将嵌套的for循环压缩成一行,因此应该优先于下面的方法。
keys = aa.keys()
packlist = list()
for ee in df.Col1.tolist():
# Here we iterate over each element of
# the dataframe's column (df.Col1)
# make a temporary list
templist = list()
for e in list(ee):
# here e is a single character of the string ee
# example: list('ABS') = ['A', 'B', 'S']
if e in keys:
# if e is one of the keys in the dict aa
# append the corresponding value to templist
templist.append(aa.get(e))
else:
# if e is not a key in the dict aa
# append e itself to templist
templist.append(e)
# append a copy of templist to packlist
packlist.append(templist.copy())
# Finally assign the list: packlist to df.Col1
# to update the column values
df.Col1 = packlist
转载仅供个人学习参考,侵删。