Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split a string column based on a logic into two new columns in pandas

I have a dataframe df :-

Type
9F,5F/6F,3T,1T
18F/19F,2F,9T,4T
17F/12F
3T
No Types

I want to generate these two additional columns based on Type column ,All wordsending with Fs seperated in one and all Ts seperated in another column respectively.If Type is No Types then No Types in both the columns:-

Type Fcol Tcol
9F,5F/6F,3T,1T 9F,5F/6F 3T,1T
18F/19F,2F,9T,4T 18F/19F,2F 9T,4T
17F/12F 17F/12F Absent
3T Absent 3T
No Types No Types No Types

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would use str.extract and a regex:

# extract F and T parts
df2 = df['Type'].str.extract('(.*\dF)?,?(\d+T.*)?')

df[['Fcol', 'Tcol']] = df2.fillna('Absent')

# identify no F nor T
m = df2.isna().all(axis=1)
# and fill original values for those
df.loc[m, ['Fcol', 'Tcol']] = df.loc[m, 'Type']

Output:

               Type        Fcol      Tcol
0    9F,5F/6F,3T,1T    9F,5F/6F     3T,1T
1  18F/19F,2F,9T,4T  18F/19F,2F     9T,4T
2           17F/12F     17F/12F    Absent
3                3T      Absent        3T
4          No Types    No Types  No Types

regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading