Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3-列中的映射列表_Python_Python 3.x_Pandas - Fatal编程技术网

Python3-列中的映射列表

Python3-列中的映射列表,python,python-3.x,pandas,Python,Python 3.x,Pandas,我试图替换pandas列中现有值的列表 team ID ... win 0 1 ... 8 1 1 ... 4 2 3 ... 5 3 4 ... 2 4 5 ... 1 团队ID可以重复,并且不是唯一的 我希望将现有列表中的团队ID更改为团队名称 team_names = ['A', 'B', 'C', 'D'] 我以前是这样做的: df.name = df.name.replace({ 1: 'A', 2:

我试图替换pandas列中现有值的列表

   team ID ... win
0     1 ...   8
1     1 ...   4
2     3 ...   5
3     4 ...   2
4     5 ...   1
团队ID可以重复,并且不是唯一的

我希望将现有列表中的团队ID更改为团队名称

team_names = ['A', 'B', 'C', 'D']
我以前是这样做的:

df.name = df.name.replace({
    1: 'A',
    2: 'B',
    3: 'C',
    4: 'D'
})
>>> dict(zip([1, 2, 3], ["a", "b", "c"]))
{1: 'a', 2: 'b', 3: 'c'}
然而,我现在面临的问题是,我想要映射的两个列表几乎有100个。所以我想知道是否有人可以分享一种更快的方法来完成这项工作?

您可以压缩这两个列表,然后将结果传递给dict:

df.element_type = df.element_type.replace(dict(zip(list1, list2)))
zip的工作原理如下:

df.name = df.name.replace({
    1: 'A',
    2: 'B',
    3: 'C',
    4: 'D'
})
>>> dict(zip([1, 2, 3], ["a", "b", "c"]))
{1: 'a', 2: 'b', 3: 'c'}
编辑 如果列表中团队名称的顺序与ID 1->A、2->B等对应,则此操作有效:

df["team ID"] = df["team ID"].replace(dict(zip(
    df["team ID"].sort_values().unique(),
    team_names
)))

此解决方案的灵感来源于此答案,可以根据您的问题进行调整。您可以使用itertools.product和string.ascii_大写字母创建一个字母列表,如['a'、'B'、'C'、…,'AA'、'AB'、'AC'],其长度与数据帧相同:

import pandas as pd
import string
import itertools

newlist = []

def iter_all_strings():
    for size in itertools.count(1):
        for s in itertools.product(string.ascii_uppercase, repeat=size):
            yield "".join(s)

for s in itertools.islice(iter_all_strings(), len(df)):
    newlist.append(s)

df['team ID'] = newlist
收益率:

  team ID  win
0       A    8
1       B    4
2       C    5
3       D    2
4       E    1
  team ID  win
0       A    8
1       A    4
2       B    5
3       C    2
4       D    1
我看到,在我撰写我的答案后,您修改了输入数据框,在团队ID列中包含了非唯一值,因此对上一条语句稍作修改将产生所需的结果注释@Edgar R.Mondragón在他的答案中首先发布了这一部分:

df['team ID'] = df["team ID"].replace(dict(zip(
    df["team ID"].sort_values().unique(),
    newlist
)))
收益率:

  team ID  win
0       A    8
1       B    4
2       C    5
3       D    2
4       E    1
  team ID  win
0       A    8
1       A    4
2       B    5
3       C    2
4       D    1

如果确实只想用相应的名称替换ID:

创建翻译词典:

dict= {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
然后替换为相应的值:

df['team_ID'] = pd.Series([dict[i] for i in list(df.team_ID) if i in list(df.team_ID)])
给出:

     team_ID  win
0       A    8
1       A    4
2       C    5
3       D    2
4       E    1

如果团队名称列表中每个团队名称的位置与团队id对应,则可以执行以下操作:

{i[0]:i[1] for i in enumerate(team_names)}
{0:A',1:B',2:C',3:D'}

用上面的dict理解替换手动词典:

df.name = df.name.replace({i[0]:i[1] for i in enumerate(team_names)})

如果我试图替换列本身中的现有列表,该怎么办?df.element\u type=df.element\u type.replacedictzip[df.element\u type.unique],[A、B、C、D]什么现有列表?请编辑您的问题,说明您的示例输入数据和预期输出。这就是你想要做的吗?那么你只是想要['A','B','C',…,'AA','AB','AC']?