Good day,
I have a dataframe where I want to isolate a part of the string for each row for that column. The problem I am having is that each row needs to have a substring of a different length, specifically I want to keep the string only up until the first occurs of "." (a period) plus the next two letters.
Example:
import pandas as pd
x = [ [ 34, 'Sydney.Au123XX'] ,
[30, 'Delhi.As1q' ] ,
[16, 'New York.US3qqa']]
x = pd.DataFrame(x)
x.columns = ["a", "b"]
#now I want to substring each row based on where "." occurs.
#I have tried the following:
y = x["b"].str.slice( stop = x["b"].str.find(".") + 2)
y = x["b"].str[0: x["b"].str.find(".")+ 2]
#desired output
desired = [[ 34, 'Sydney.Au'] ,
[30, 'Delhi.As' ] ,
[16, 'New York.US'] ]
desired = pd.DataFrame(desired )
desired .columns = ["a", "b"]
Please see my code for the desired output.
I do not want to use a loop.
Thanks in advance.