[英]Python pandas df rename index using regex
My df looks like this.我的 df 看起来像这样。
2019 2018
Sally#16# -6461 -6340
Brian#06# 7139 7200
rebecca#9# 1337 1067
mark#10# 10922 11128
toto 12936 13054
Here, I would like to remove #number# from all my indexes.在这里,我想从我的所有索引中删除#number#。 Is there a way to achieve this?
有没有办法做到这一点? probably with regex, so that I can have a final df like this:
可能使用正则表达式,这样我就可以有一个像这样的最终 df:
2019 2018
Sally -6461 -6340
Brian 7139 7200
rebecca 1337 1067
mark 10922 11128
toto 12936 13054
Thank you!谢谢!
Use Series.str.replace
with \d+
for select all numbers between #
:使用
Series.str.replace
与\d+
为 select #
之间的所有数字:
df.index = df.index.str.replace(r'(#\d+#)', '')
print (df)
2019 2018
Sally -6461 -6340
Brian 7139 7200
rebecca 1337 1067
mark 10922 11128
toto 12936 13054
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.