用一个di来重映射pandas列的值

2024-04-26 23:09:00 发布

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

我有一本这样的字典:di = {1: "A", 2: "B"}

我想将其应用于数据帧的“col1”列,类似于:

     col1   col2
0       w      a
1       1      2
2       2    NaN

得到:

     col1   col2
0       w      a
1       A      2
2       B    NaN

我怎样才能做到最好?出于某种原因,与此相关的google术语只显示了如何从dict创建列的链接,反之亦然:-/


Tags: 数据字典链接googlenandictcol2col1
3条回答

你的问题有点模棱两可。至少有三种解释:

  1. di中的键引用索引值
  2. di中的键是指df['col1']
  3. di中的键指的是索引位置(不是OP的问题,而是为了好玩而抛出的)

以下是每种情况的解决方案。


案例1: 如果di的键是指索引值,则可以使用update方法:

df['col1'].update(pd.Series(di))

例如

import pandas as pd
import numpy as np

df = pd.DataFrame({'col1':['w', 10, 20],
                   'col2': ['a', 30, np.nan]},
                  index=[1,2,0])
#   col1 col2
# 1    w    a
# 2   10   30
# 0   20  NaN

di = {0: "A", 2: "B"}

# The value at the 0-index is mapped to 'A', the value at the 2-index is mapped to 'B'
df['col1'].update(pd.Series(di))
print(df)

收益率

  col1 col2
1    w    a
2    B   30
0    A  NaN

我已经修改了您原始帖子中的值,因此可以更清楚地看到update在做什么。 注意di中的键如何与索引值关联。索引值的顺序(即索引位置)无关紧要。


案例2: 如果di中的键引用df['col1']值,则@DanAllan和@DSM将演示如何使用replace实现此目标:

import pandas as pd
import numpy as np

df = pd.DataFrame({'col1':['w', 10, 20],
                   'col2': ['a', 30, np.nan]},
                  index=[1,2,0])
print(df)
#   col1 col2
# 1    w    a
# 2   10   30
# 0   20  NaN

di = {10: "A", 20: "B"}

# The values 10 and 20 are replaced by 'A' and 'B'
df['col1'].replace(di, inplace=True)
print(df)

收益率

  col1 col2
1    w    a
2    A   30
0    B  NaN

注意在这种情况下,di中的键是如何更改的,以匹配df['col1']中的


案例3: 如果di中的键引用索引位置,则可以使用

df['col1'].put(di.keys(), di.values())

自从

df = pd.DataFrame({'col1':['w', 10, 20],
                   'col2': ['a', 30, np.nan]},
                  index=[1,2,0])
di = {0: "A", 2: "B"}

# The values at the 0 and 2 index locations are replaced by 'A' and 'B'
df['col1'].put(di.keys(), di.values())
print(df)

收益率

  col1 col2
1    A    a
2   10   30
0    B  NaN

这里,修改了第一行和第三行,因为di中的键是02,使用Python基于0的索引,这些键是指第一和第三个位置。

您可以使用^{}。例如:

>>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
>>> di = {1: "A", 2: "B"}
>>> df
  col1 col2
0    w    a
1    1    2
2    2  NaN
>>> df.replace({"col1": di})
  col1 col2
0    w    a
1    A    2
2    B  NaN

或者直接在^{}上,即df["col1"].replace(di, inplace=True)

mapreplace

快得多

如果您的字典有多个键,那么使用map比使用replace快得多。此方法有两种版本,具体取决于词典是否详尽地映射了所有可能的值(以及是否希望不匹配项保留其值或转换为NaNs):

穷举映射

在这种情况下,形式非常简单:

df['col1'].map(di)       # note: if the dictionary does not exhaustively map all
                         # entries then non-matched entries are changed to NaNs

尽管map最常用的参数是函数,但它也可以使用字典或序列:Documentation for Pandas.series.map

非穷尽映射

如果您有一个非穷举映射,并且希望保留不匹配的现有变量,则可以添加fillna

df['col1'].map(di).fillna(df['col1'])

正如@jpp在这里的回答:Replace values in a pandas series via dictionary efficiently

基准

在pandas 0.23.1版中使用以下数据:

di = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H" }
df = pd.DataFrame({ 'col1': np.random.choice( range(1,9), 100000 ) })

%timeit进行测试,发现mapreplace快大约10倍。

请注意,map的加速将随数据的变化而变化。最大的加速似乎是大型词典和详尽的替代品。更多的基准测试和讨论请参见@jpp answer(链接在上面)。

相关问题 更多 >