简体   繁体   中英

Replace part of string globally in dataframe

I have following dataframe:

id   feature
1    p.Asp25Asn
2    p.Gly25Asn
3    p.Ile20Tyr

I would like to replace a part of column 'feature'.

For example: Asp when matched should be replaed by 'D'. Asn when matched should be replace by 'N' and so on for other column values like Gly --> G, Ile -->I, Tyr -->Y

The expected output is:

id   feature
1    p.D25N
2    p.G25N
3    p.I20Y

I am writing a function which matches a condition and perform such replacement globally. If there are better solution available, please suggest.

Thanks

you can use pandas.DataFrame.replace using a dictionary.

df=pd.DataFrame({'id':[1,2,3],'feature':['p.Asp25Asn','p.Gly25Asn','p.Ile20Tyr']})
>>>df
   id     feature
0   1  p.Asp25Asn
1   2  p.Gly25Asn
2   3  p.Ile20Tyr

mydict={'Asp':'D','Asn':'N','Gly':'G','Ile':'I','Tyr':'Y'}
df = df.replace({"feature": mydict},regex=True)
>>>df
   id   feature
0   1    p.D25N
1   2    p.G25N
2   3    p.I20Y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM