I have a dataframe in pandas, with columns named "string_string", I'm trying to rename them by removing the "_" and the following string. For example, I want to change "12527_AC9E5" to "12527". I've tried to use various replace options, and I can replace a specific part of the string (eg, I can replace all the "_"), but when I introduce wildcards I do not achieve the desired result.
Below are some of the things I thought would work, but don't. If I remove the wild cards they work (ie, they replace the _).
df = df.rename(columns=lambda x: x.sub('_.+', ''))
df.columns = df.columns.str.replace('_.+','')
Any help appreciated
Just split on '_' and take the first element. You can take advantage of dictionary comprehension:
df = df.rename(columns={col: col.split('_')[0] for col in df.columns})
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.