Python Forum
extract from strings some parts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract from strings some parts
#1
Hi,
I have a data frame where a specific columns is filled in with long strings. I want for each entry there to extract a specific word if that word has some of the following attributes:

-word ends with strasse. Then export the whole word until the comma.
-word starts with a capital A. The export the whole word until the comma.
-words ends gasse. Then export the whole word until the comma.
-word ends with weg. The export the whole word until the comma.
-word starts with a capital A, return the whole word until the comma.

For example for the string
"A12, Thörishaus, Köniz, Verwaltungskreis"

return A12 (last rule)

For example for the string
"Stapfenstrasse, Köniz, Verwaltungskreis"

return Stapfenstrasse (first rule).

How I can do something like that.

So for each entry in dataframe column check if one of the rules apply and then export the string needed.

Can you please advice?

Thanks
Alex
Reply
#2
split the string into list

mstring = "A12, Thörishaus, Köniz, Verwaltungskreis"
### get List from string
mlist  = mstring.split(",")
### get first value from list
print(mlist[0])
Result:
Output:
A12
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 695 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,018 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Extract parts of a log-file and put it in a dataframe hasiro 4 6,120 Apr-08-2022, 01:18 PM
Last Post: hasiro
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  extract particular strings into a variable lokesh 3 2,061 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Finding multiple strings between the two same strings Slither 1 2,477 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Extract Strings From Text File - Out Put Results to Individual Files dj99 8 4,849 Jun-28-2018, 10:41 AM
Last Post: dj99
  lists, strings, and byte strings Skaperen 2 4,180 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020