[판다스] 원천 데이터 프레임의 <>안의 문자만 뽑아 새로운 데이터 프레임 생성
조회수 311회
데이터 프레임의 sentence의 다음과 같은 데이터 에서
df_sentence
<Donald Trump:PS> is <America:LC> President. He came to <Japan:OG> in <July 20:DT>
<NC Soft:OG>is established in <Match, 1993:DT>
다음과 같은 결과를 추출(데이터프레임으로) 하고 싶습니다.
PS LC DT OG
DonaldTrump America 20-Jul
Japan Match, 1993 NC Soft
1 답변
-
import re # Define the regular expression pattern for entity-label pairs pattern = r'<(.+?):(.+?)>' # Define the input text text = """<Donald Trump:PS> is <America:LC> President. He came to <Japan:OG> in <July 20:DT> <NC Soft:OG>is established in <March, 1993:DT>""" # Find all the matches in the text matches = re.finditer(pattern, text) # Iterate over the matches and print the entity-label pairs for match in matches: entity = match.group(1) label = match.group(2) print((entity, label))
- 와 너무 깔끔하게 처리가 되네요 감사합니다. hun0520@gmail.com 2022.12.22 10:32
- chat.openai 의 답변이었습니다! nowp 2022.12.23 09:17
댓글 입력