Reputation: 13
I have data like this along with other columns in a pandas df. Apologies I haven't figured out how to present the question with code for the dataframe. First Post
Location: - Tokyo, Japan - Sacramento, USA - Mexico City, Mexico - Mexico City, Mexico - Colorado Springs, USA - New York, USA - Chicago, USA
Does anyone know how I could isolate the country name from the location and create a new column with just the Country Name?
Upvotes: 1
Views: 65
Reputation: 1139
You can do this without any regular expressions - you can find the String.indexOf(“, “) to find the position of the seperator in the String, and then use String.substring to cut the String down to just this section.
However, a regular expression can also do this easily, but would likely be slower.
Upvotes: 0
Reputation: 34046
Try this:
In [29]: pd.DataFrame(df.Location.str.split(',',1).tolist(), columns = ['City','Country'])
Out[29]:
City Country
0 Tokyo Japan
1 Sacramento USA
2 Mexico City Mexico
3 Mexico City Mexico
4 Colorado Springs USA
5 Seoul South Korea
Upvotes: 1