我的数据框具有以下值,我想删除最后一个字符,即 - 从所有行中删除。我该怎么做?
df:
Sn URL
1 Sunil-
2 R-amesh-
3 Oxa--
4 --AB
我有以下功能,如何应用它?可以使用lambda吗?请帮忙?
def rchop(thestring, ending):
if thestring.str.endswith(ending):
return thestring[:-len(ending)]
return thestring
df['URL'] = rchop(df['URL'], '-') -- not working
预期输出:
Sn URL
1 Sunil
2 R-amesh
3 Oxa
4 --AB
最佳答案
我们可以使用Series.str.rstrip
In [16]: df['URL'] = df['URL'].str.rstrip('-')
In [17]: df
Out[17]:
Sn URL
0 1 Sunil
1 2 R-amesh
2 3 Oxa
3 4 --AB
关于python - 从数据框列值末尾删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756236/