使用正则表达式匹配替换索引值

问题描述:

我试图在熊猫系列内的索引中进行字符串替换。但是,目前它不匹配或查找子字符串并将其替换为给定值。使用正则表达式匹配替换索引值

我目前的系列:

index @12456 string_1 @54324 string_2 @34566 string_3 @57453 string_4 @67645 string_5 Name: value, dtype: object

为了这个,我试图从索引值删除 '@' 符号。

我使用:

series.replace(to_replace={'index': {'@': ''}}) 

但是,它似乎并不匹配字符串,返回初始系列。我错过了什么,如何达到预期的结果?

我的熊猫版本目前为0.15。

P.S.我也曾尝试:

series.replace(to_replace={'index': {r'@': ''}}) 
series.replace(to_replace={'index': {r'\@': ''}}) 

UPDATE

一些答案在未来解决的具体问题,但我需要一个更一般的情况。因此,如果该系列是:

index other_index @12456 1 string_1 @54324 2 string_2 @34566 3 string_3 @57453 4 string_4 @67645 5 string_5 Name: value, dtype: object

如何将适用同样的操作这里的指数?这对第一项措施和其他措施都有效?

你可以这样做:

series.index = series.index.map(lambda v: v.replace('@', '')) 

series.index = series.index.str.replace('@', '') 

对于多指标,这里是一个可能的解决方案(不漂亮,虽然):

# setting up the indices and the series 
arrays = [['@str1', '@str2'], [1, 2]] 
ind = pd.MultiIndex.from_arrays(arrays, names=['index', 'other_index']) 
series = pd.Series(['s1', 's2'], index=ind) 

# index other_index 
# @str1 1    s1 
# @str2 2    s2 
# dtype: object 

vals = zip(*series.index.get_values()) ## values of indices reshaped into a list of tuples 
# [('@str1', '@str2'), (1L, 2L)] 

# find out where is the index that we want to change 
pos = series.index.names.index('index') 
# now we can modify the tuple by replacing the strings we do not want 
vals[pos] = tuple([x.replace('@', '') for x in vals[pos]]) 

# Re-create the multi-index 
series.index = pd.MultiIndex.from_arrays(vals, names=series.index.names) 

print series 
# index other_index 
# str1 1    s1 
# str2 2    s2 
# dtype: object 
+0

我需要为了能够匹配任何特定字符,不幸 – Rambatino

+0

然后动态分配'Julien Spronck'的代码中的'@' 。 x是你的符号:series.index = series.index.map(lambda v:v.replace(x,'')) – RandomHash

+0

@Rambatino我改变了这个解决方案,使用多索引...让我知道它是否有帮助 –