比较两列并保持字符串之间的差异

时间:2020-10-21 19:29:19

标签: python regex pandas dictionary replace

在将熊猫从0.25.1升级到1.1.3之前,我能够从中剥离数据帧中的字符串:

+---+------------------+------------------+
|   |   strip_me       |     pattern      |
+---+------------------+------------------+
| 0 |   string part 0  |     string       |
+---+------------------+------------------+
| 1 |   string part 1  |     part         |
+---+------------------+------------------+

对此:

+---+------------------+------------------+
|   |   strip_me       |     pattern      |
+---+------------------+------------------+
| 0 |   part 0         |     string       |
+---+------------------+------------------+
| 1 |   string 1       |     part         |
+---+------------------+------------------+

使用如下操作:

df['strip_me'] = df['strip_me'].replace(''+df.pattern,'',regex=True)

但是自更新以来,我的jupyter笔记本返回此错误:

ValueError: Series.replace cannot use dict-like to_replace and non-None value

我想念什么?通过stackoverflow和ggl进行的彻底搜索尚未提供答案。我什至尝试过RTF-pandas-M。您将采取什么方法来理解和解决ValueError? 我在Anaconda中有两种调试环境,一种使用旧熊猫,另一种使用升级的熊猫。由于堆栈内的依赖性,不能选择降级。

我会很高兴能帮助我找到解决方案。

1 个答案:

答案 0 :(得分:1)

我认为系列参数被视为索引为键的字典。现在,字典选项仅可用于DataFrame。对于Series,字典k:v表示其他含义(k表示将值v替换为值df['strip_me'])。

快速解决方案:使用以下方法将您的df['strip_me'] = (df[['strip_me']].T.replace(''+df.pattern,'',regex=True) .loc['strip_me'] ) 转换为数据框:

int absNum = obj."Absolute Number"

string s = "Hello " absNum "!"
if (absNum == 1 ) {
   s = null
}
display s
相关问题