-1

How can i strip the string below?(I just want to create a column to show city and state,you can see the city and state's name after \n)

df['Address']
array(['208 Michael Ferry Apt. 674\nLaurabury, NE 37010-5101',
       '188 Johnson Views Suite 079\nKathleen, CA 48958',
       '9127 Elizabeth Stravenue\nDanieltown, WI 06482-3489', ...,
       '4215 Tracy Garden Suite 076\nJoshualand, VA 01707-9165',
       'USS Wallace\n**FPO AE** 73316',
       '37778 George Ridges Apt. 509\nEast Holly, NV 29290-3595'],
      dtype=object)
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
EK.
  • 3
  • 1
  • are you searching for something like str.split()? https://docs.python.org/3/library/stdtypes.html#str.split – Hoxha Alban May 23 '21 at 15:08
  • Actually I see new built-in function which is removeprefix that I don't see before. It can be helpful. Thank you – EK. May 23 '21 at 15:15

1 Answers1

0

You can use regex expressions to get the city name, in your case it would look something like this:

    import re
    example_string = '208 Michael Ferry Apt. 674\nLaurabury, NE 37010-5101'
    
    city = re.findall(r"\n(.*?),", text)
    
    print(city)

However, given that you have ... in your array, will probably cause problems, since that's not a string.

JhonnyB
  • 41
  • 4