Split a pandas dataframe column into multiple based on text values

I have a pandas dataframe with a column.

id    text_col
1     Was it Accurate?: YesnnReasoning: This is a sample : text
2     Was it Accurate?: YesnnReasoning: This is a :sample 2 text
3     Was it Accurate?: NonnReasoning: This is a sample: 1. text

I have to break the text_col into two columms "Was it accurate?" and "Reasoning"

The final dataframe should look like:

id    Was it Accurate?    Reasoning
1     Yes             This is a sample : text
2     Yes             This is a :sample 2 text
3     No              This is a sample: 1. text

The text values can have multiple : “colons” in it

I tried splitting the text_col using “nnReasoning:” but did’nt get desired result.It is leaving out the text after second colon (:)

df[['Was it Accurate?', 'Reasoning']] = df['text_col'].str.extract(r'Was it Accurate?: (Yes|No)nnReasoning: (.*)')

Edit:

I applied the function on the LLM_response column of my sample_100 dataframe. and printed the first row. if you see closely the sample_100.iloc[0]['Reasoning'] has stripped off all the text after :

Temp dict obj to test on:

{'id_no': [8736215],
 'Notes': [' Temp Notes Sample xxxxxxxxxxxxx [4/21/23, 2:10 PM]  Work started -work complete-'],
 'ProblemDescription': ['Sample problem description xxxxxxxxxxxxxxxxxxxxxxxx'],
 'LLM_response': ['Accurate & Understandable: YesnnReasoning: The Technician notes are accurate and understandable as:n1) The technician provided detailed steps on how they addressed the mold issue by removing materials, treating surfaces, priming, and painting them.n2) Additionally, even though there was non-repair related information (toilet repairs), the main issue of mold growth was addressed.n3) The process described logically follows the process for remedying a mold issue, which aligns with the problem description.'],
 'Accurate & Understandable': ['Yes'],
 'Reasoning': ['The Technician notes are accurate and understandable as:']}

4

The issue is not due to colons, but to newlines in your sample text. Those are not matched by . by default. You should add the re.DOTALL flag.

Example:

import re
import pandas as pd

df = pd.DataFrame({'id': [1, 2, 3],
                   'text_col': ['Was it Accurate?: YesnnReasoning: This is a sample: text',
                                'Was it Accurate?: YesnnReasoning: This is a sample:n with newline',
                                'Was it Accurate?: NonnReasoning: This is a sample text']})

df[['Was it Accurate?', 'Reasoning']] = (df['text_col']
                                         .str.extract(r'Was it Accurate?: (Yes|No)nnReasoning: (.*)',
                                                      flags=re.DOTALL)
                                        )

Output:

   id                                                              text_col Was it Accurate?                         Reasoning
0   1            Was it Accurate?: YesnnReasoning: This is a sample: text              Yes            This is a sample: text
1   2  Was it Accurate?: YesnnReasoning: This is a sample:n with newline              Yes  This is a sample:n with newline
2   3              Was it Accurate?: NonnReasoning: This is a sample text               No             This is a sample text

one way to handle it could be with the split function.

# deal with the first column by splitting on 'n' and take the first value then split on ':' and take the second one
df['Was it Accurate ?'] = df.apply(lambda x:x.split('n')[0].split(':')[1],axis=1)
# again split on 'n' then on ':' but put back the ':' if you have many colons in the text. 
df['Reasoning'] = df.apply(lambda x: ':'.join(x.split('n)[2].split(':')[1:]), axis = 1)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật