英文:
Pandas remove character from string in column NOT containing substring
问题
slate['start'] = slate['start'].astype(str).str.replace('-', '')
这行代码会删除数据框中“start”列中不包含“BC”的每个单元格中的“-”。
英文:
I want to remove "-" in each cell of a column of a dataframe that does not contain "BC".
I do not know how to tweak the following code to "not containing" instead of "containing"
Any help is appreciated.
slate['start'] = slate['start'].astype(str).str.findall('BC'.replace("-",""))
This worked for the inverse case of: replace x with y if cell contains Z.
slate['start'] = slate['start'].astype(str).str.findall(~'BC'.replace("-",""))
I then added the tilde (in my frustration) which clearly did not fork for strings - it yielded a TypeError.
I do not know of any neat way to implement this.
My df is of this "style"
name municipality start end ext x y
0 La Coma Altafulla -200 AD 125 AD 1.74 1.379991 41.154995
答案1
得分: 1
你可以使用 pd.Series.where()
,它会将条件(第一个参数)为假的值替换为第二个参数中的值:
slate.start.where(slate.start.str.contains('BC'),
slate.start.str.replace('-', ''),
inplace=True)
英文:
You can use pd.Series.where()
, which replaces values where the condition (the first argument) is false with the values from the second argument:
slate.start.where(slate.start.str.contains('BC'),
slate.start.str.replace('-', ''),
inplace=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论