Pandas:使用字典替换与其他列中的条件匹配的一列中的单元格中的值

时间:2018-02-14 17:41:36

标签: python python-3.x pandas dictionary dataframe

采用以下示例数据框:

df = pd.DataFrame([['00100', 'Alpha', None],
                   ['00100', 'Beta', None],
                   ['05300', 'Theta', None],
                   ['95687', 'Gamma', None],
                   ['05300', 'Sigma', None]])

看起来像

       0      1     2
0  00100  Alpha  None
1  00100   Beta  None
2  05300  Theta  None
3  95687  Gamma  None
4  05300  Sigma  None

我有一个字典,它根据第0列映射第2列的值:

match = {
    '00100' : '09010',
    '05300' : '09004'
}

我想根据第0列的值更改第2列中的值。在将dict与数据帧匹配后,我期望的最终结果是:

       0      1      2
0  00100  Alpha  09010
1  00100   Beta  09010
2  05300  Theta   None
3  95687  Gamma  09004
4  05300  Sigma  09004

起初我认为我可以使用.loc以下列方式创建.apply函数或for循环:

df.loc[df[0] == match[key]][2] = match[value]

但这引起了例外:

"A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead"

我似乎无法理解如何申请此特定案例。

我正在使用: Python 3.6.1 熊猫0.20.1

2 个答案:

答案 0 :(得分:2)

试试这个。 pd.Series.map需要function, dict, or Series

df[2] = df[0].map(match).fillna(df[2])

#        0      1      2
# 0  00100  Alpha  09010
# 1  00100   Beta  09010
# 2  05300  Theta  09004
# 3  95687  Gamma   None
# 4  05300  Sigma  09004

答案 1 :(得分:0)

这是一个可能的解决方案:

df[2] = df[0].apply(lambda x: match[x] if x in match else None)