Pandas 在不包含子字符串的列中删除字符串中的字符

huangapple go评论67阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年2月24日 04:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定