I have a dataframe that has one column with data that looks like this:
AAH.
AAH.
AAR.UN
AAR.UN
AAR.UN
AAR.UN
AAV.
AAV.
AAV.
I think I need to use the apply method to trim the column data. So if there is anything after the period keep the data unchanged but if there is nothing after the period then return just the letters without the period at the end. I know I can probably use a lambda function and maybe a string split or something to do this but have not much of an idea to make it happen.
This is kind of what I have so far:
df.apply(lambda x: string.split('.'))
I am not sure if I can use an if statement or something with the lambda function this way?
Any guidance appreciated.
You can also use lambda function to do this:
Since there's only one column, you can take advantage of vectorized string operations via
.str
(docs):Otherwise you'd have to do something like
df.applymap(lambda x: x.rstrip("."))
, or drop down to numpychar
methods.I applied a simple function to the column to manipulate all string values, for somewhat similar problem.