如何从字典中替换dataframe列中的特定值?

2024-09-09 00:27:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有一张如下的表格:

Col1     Col2
ABS      45
CDC      23
POP      15

现在,我有一本字典。所以对于匹配的关键部分,我只希望列中的值改变。对于与字典不匹配的其他字母,键应保持不变。你知道吗

最后一个表应该如下所示:

Col1     Col2
ADBS     45
LCDLC    23
PLOPL    15

我试图使用以下代码,但它不工作。你知道吗

df['Col1'].str.extract[r'([A-Z]+)'].map(aa)

Tags: 代码df字典字母abspop关键表格
2条回答

您可以使用下面的替换来完成此操作

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]))

解决方案

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

输出

enter image description here

Unpacking the Condensed List Comprehension

让我们用一种更易读的形式写下清单。我们创建一个函数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']

So what is the core logic?

The following code-block shows you step-by-step how the logic works. Obviously, the list comprehension introduced at the beginning of the solution compresses the nested for loops into a single line, and hence should be preferred over the following.

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

参考文献

列表和dict理解是一些非常强大的工具,任何python程序员在编写代码时都会发现它们既方便又漂亮。它们能够将原本复杂的代码块整齐地压缩成一两行。我建议你看一下下面的内容。你知道吗

  1. List Comprehensions: ^{}
  2. Dict Comprehensions: ^{}
  3. List Comprehension in Python: ^{}

相关问题 更多 >