python - pandas vectorized string replace by index -
trying change first 2 characters in pandas column string.
def shift(x): x=list(x) x[0] = '1' x[1] = '9' print x #this prints correct result list ##str(x) return ''.join(x) ## works mc_data['timeshift'] = mc_data['realtime'].map(lambda x: shift(x) )
output nonetype
i tried, str.slice_replace.map(lambda x: '19')
.
how do this?
instead of replacing first 2 characters, build new strings:
>>> df = pd.dataframe({"a": ['abcd']*4}) >>> df 0 abcd 1 abcd 2 abcd 3 abcd >>> df["a"] = "19" + df["a"].str[2:] >>> df 0 19cd 1 19cd 2 19cd 3 19cd
Comments
Post a Comment