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
输出:
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()]
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
您可以使用下面的替换来完成此操作
解决方案
输出:
让我们用一种更易读的形式写下清单。我们创建一个函数
do_something
来理解列表理解的第一部分中发生了什么。第二部分(for ee in df.Col1.tolist()
)基本上迭代数据帧df
的列'Col1'
中的每一行。你知道吗拆包
do_something(x)
函数
do_something(x)
执行以下操作。如果你用x = 'ABS'
试试,会更容易。do_something
中的''.join(some_list)
加入生成的列表。下面的代码块将说明这一点。你知道吗输出:
参考文献
列表和dict理解是一些非常强大的工具,任何python程序员在编写代码时都会发现它们既方便又漂亮。它们能够将原本复杂的代码块整齐地压缩成一两行。我建议你看一下下面的内容。你知道吗
相关问题 更多 >
编程相关推荐