【问题标题】:Python pandas equivalent for replacePython pandas 等效于替换
【发布时间】:2012-08-22 13:47:39
【问题描述】:

在 R 中,有一个相当有用的 replace 函数。 本质上,它在数据框的给定列中进行有条件的重新分配。 它可以这样使用: replace(df$column, df$column==1,'Type 1');

在 pandas 中实现相同目标的好方法是什么?

我应该将 lambda 与 apply 一起使用吗? (如果是这样,我如何获得对给定列的引用,而不是整行)。

我应该在data_frame.values 上使用np.where 吗? 似乎我在这里遗漏了一个非常明显的东西。

欢迎提出任何建议。

【问题讨论】:

    标签: python pandas equivalent


    【解决方案1】:

    pandas 也有一个replace 方法:

    In [25]: df = DataFrame({1: [2,3,4], 2: [3,4,5]})
    
    In [26]: df
    Out[26]: 
       1  2
    0  2  3
    1  3  4
    2  4  5
    
    In [27]: df[2]
    Out[27]: 
    0    3
    1    4
    2    5
    Name: 2
    
    In [28]: df[2].replace(4, 17)
    Out[28]: 
    0     3
    1    17
    2     5
    Name: 2
    
    In [29]: df[2].replace(4, 17, inplace=True)
    Out[29]: 
    0     3
    1    17
    2     5
    Name: 2
    
    In [30]: df
    Out[30]: 
       1   2
    0  2   3
    1  3  17
    2  4   5
    

    或者你可以使用numpy-style 高级索引:

    In [47]: df[1]
    Out[47]: 
    0    2
    1    3
    2    4
    Name: 1
    
    In [48]: df[1] == 4
    Out[48]: 
    0    False
    1    False
    2     True
    Name: 1
    
    In [49]: df[1][df[1] == 4]
    Out[49]: 
    2    4
    Name: 1
    
    In [50]: df[1][df[1] == 4] = 19
    
    In [51]: df
    Out[51]: 
        1   2
    0   2   3
    1   3  17
    2  19   5
    

    【讨论】:

    • 没有认真阅读手册让我很痛苦。
    • 老实说,我也几乎从不阅读手册,直到某些事情真正让我感到困惑。但是使用像 IPython 这样的智能解释器的一个优点是,您可以构建像 df 这样的对象,然后使用 tab-completion 来查看其中存在哪些方法。
    • 确实如此。 iPython 是一个美丽的东西。在我的辩护中,替换功能没有列出here
    • 嘿!也许我从不阅读手册的政策比我想象的更有意义! :^)
    • 虽然是here =P
    【解决方案2】:

    Pandas doc for replace 没有例子,这里就举几个。对于那些从 R 的角度来看的人(比如我),replace 基本上是一个通用的替换函数,它结合了 R 函数 plyr::mapvaluesplyr::revaluestringr::str_replace_all 的功能。由于 DSM 涵盖了单值的情况,我将介绍多值的情况。

    示例系列

    In [10]: x = pd.Series([1, 2, 3, 4])
    
    In [11]: x
    Out[11]: 
    0    1
    1    2
    2    3
    3    4
    dtype: int64
    

    我们想用负整数替换正整数(而不是乘以 -1)。

    两个值列表

    一种方法是通过一个列表(或 pandas 系列)我们想要替换的值和第二个列表来替换我们想要替换的值。

    In [14]: x.replace([1, 2, 3, 4], [-1, -2, -3, -4])
    Out[14]: 
    0   -1
    1   -2
    2   -3
    3   -4
    dtype: int64
    

    这对应于plyr::mapvalues

    值对字典

    有时,拥有一个值对字典会更方便。索引是我们替换的那个,值是我们替换它的那个。

    In [15]: x.replace({1: -1, 2: -2, 3: -3, 4: -4})
    Out[15]: 
    0   -1
    1   -2
    2   -3
    3   -4
    dtype: int64
    

    这对应于plyr::revalue

    字符串

    它对字符串的工作方式类似,除了我们还可以选择使用正则表达式模式。

    如果我们只是想用其他字符串替换字符串,它的工作原理和以前完全一样:

    In [18]: s = pd.Series(["ape", "monkey", "seagull"])
    In [22]: s
    Out[22]: 
    0        ape
    1     monkey
    2    seagull
    dtype: object
    

    两个列表

    In [25]: s.replace(["ape", "monkey"], ["lion", "panda"])
    Out[25]: 
    0       lion
    1      panda
    2    seagull
    dtype: object
    

    字典

    In [26]: s.replace({"ape": "lion", "monkey": "panda"})
    Out[26]: 
    0       lion
    1      panda
    2    seagull
    dtype: object
    

    正则表达式

    将所有as 替换为xs。

    In [27]: s.replace("a", "x", regex=True)
    Out[27]: 
    0        xpe
    1     monkey
    2    sexgull
    dtype: object
    

    将所有ls 替换为xs。

    In [28]: s.replace("l", "x", regex=True)
    Out[28]: 
    0        ape
    1     monkey
    2    seaguxx
    dtype: object
    

    注意seagull 中的两个ls 都被替换了。

    as 替换为xs,将ls 替换为ps

    In [29]: s.replace(["a", "l"], ["x", "p"], regex=True)
    Out[29]: 
    0        xpe
    1     monkey
    2    sexgupp
    dtype: object
    

    在想用相同的值替换多个不同的值的特殊情况下,可以只用一个字符串作为替换。它不能在列表中。将as 和ls 替换为ps

    In [29]: s.replace(["a", "l"], "p", regex=True)
    Out[29]: 
    0        ppe
    1     monkey
    2    sepgupp
    dtype: object
    

    (归功于 cmets 中的 DaveL17)

    【讨论】:

    • +1 以获得一系列不错的示例。对于未来的访问者,您还可以用单个值 s.replace(["a", "l"], "x", regex=True) 替换多个值,但单个替换值不能在列表中(“from”和“to”列表必须具有相同的值才能工作。)跨度>
    • 我添加了你的例子。
    • 干杯。我无法再编辑上面的评论,但最好将其描述为('from' 和 'to' 列表必须具有相等的 length 才能工作。)
    猜你喜欢
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2015-04-26
    • 2022-01-04
    • 1970-01-01
    • 2022-01-24
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多