2014-06-07 49 views
0

试图更改熊猫列字符串中的前两个字符。熊猫矢量化字符串替换索引

def shift(x): 
x=list(x) 
x[0] = '1' 
x[1] = '9' 
print x #this prints the correct result as list 
##str(x) 
return ''.join(x) ## this works 
mc_data['timeshift'] = mc_data['realtime'].map(lambda x: shift(x)) 

输出NoneType

我也试过,str.slice_replace.map(lambda x: '19')

我该怎么做?

回答

0

而不是更换前两个字符,你可以建立新的字符串:

>>> df = pd.DataFrame({"A": ['abcd']*4}) 
>>> df 
     A 
0 abcd 
1 abcd 
2 abcd 
3 abcd 
>>> df["A"] = "19" + df["A"].str[2:] 
>>> df 
     A 
0 19cd 
1 19cd 
2 19cd 
3 19cd