jog*_*oga 5 python dictionary replace pandas
我想Series用字典替换大熊猫中的值.我正在关注@ DSM 接受的答案:
s = Series(['abc', 'abe', 'abg'])
d = {'b': 'B'}
s.replace(d)
Run Code Online (Sandbox Code Playgroud)
但这没有任何影响:
0 abc
1 abe
2 abg
dtype: object
Run Code Online (Sandbox Code Playgroud)
该文档解释了所需的dict格式DataFrames(即嵌套的dicts与顶级键对应的列名),但我看不到任何特定的Series.
你可以使用regex=True参数:
In [37]: s.replace(d, regex=True)
Out[37]:
0 aBc
1 aBe
2 aBg
dtype: object
Run Code Online (Sandbox Code Playgroud)
正如您已经发现的那样 - 它是RegEx的替代品,它不会像您预期的那样工作:
In [36]: s.replace(d)
Out[36]:
0 abc
1 abe
2 abg
dtype: object
Run Code Online (Sandbox Code Playgroud)
这是按预期工作:
In [38]: s.replace({'abc':'ABC'})
Out[38]:
0 ABC
1 abe
2 abg
dtype: object
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6331 次 |
| 最近记录: |