简体   繁体   English

从数据框列值的末尾删除字符

[英]Remove characters from the end of the data frame column values

I have dataframe with below values and I want to remove last characters ie - from all the row. 我有以下值的数据框,我想从所有行中删除最后一个字符,即-。 How can I do it? 我该怎么做?

df: DF:

Sn  URL
1   Sunil-
2   R-amesh-
3   Oxa--
4   --AB

I have below function, how to apply this? 我有以下功能,如何应用呢? Is is it possible to use lambda? 是否可以使用lambda? Please help? 请帮忙?

def rchop(thestring, ending):
    if thestring.str.endswith(ending):
       return thestring[:-len(ending)]
    return thestring

df['URL'] = rchop(df['URL'], '-') -- not working

Output expected: 预期输出:

Sn  URL
1   Sunil
2   R-amesh
3   Oxa
4   --AB

we can use Series.str.rstrip 我们可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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