python 列表理解 return 基于字母的名称

python list comprehension return names based on letter

我有一个 DataFrame:

df = pd.DataFrame(['A','B','C'], columns = ['Letters'])

我有一个列表 names:

names = ['George All', 'George Ball','George Ago','George Call']

如何在我的 DataFrame 中创建一个新列,其中包含姓氏以 Letters 列开头的姓名列表。

例如:

Letters  Names
A        ['George All','George Ago']
B        George Ball
C        George Call

这是我现在拥有的:

df['Names'] = [name for name in names if (name.split()[1][0] == df['Letters'])]
>>> df['Names'] = [[n for n in names if n.split()[1][0] == x] for x in df['Letters']]
>>> df
  Letters                     Names
0       A  [George All, George Ago]
1       B             [George Ball]
2       C             [George Call]

[3 rows x 2 columns]

您可以通过预先按首字母对所有名称进行分组来更有效地执行此操作(如果 names 是一个大列表则很重要)。

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for item in names:
...     d[item.split()[1][0]].append(item)
... 
>>> df['Names'] = [d[x] for x in df['Letters']]
>>> df
  Letters                     Names
0       A  [George All, George Ago]
1       B             [George Ball]
2       C             [George Call]