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

Pandas result of findall to single row

hello I have csv file and I using pandas and my issue is when I using
pandas.Series.str.findall. What I wont is after call findall I would like to save value of result (what is array) to row in csv

this is my code


 data = pd.read_csv("input.csv")
 data["specificText"] = data["specificText"].str.findall(patternString)
data.to_csv("exportF.csv")


my input csv looks like

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

id, specificText

A1A, DEF_2122 bla bla DEF_87653 blla
A2A, DEF_7654 bla bla DEF_2199 blla
X1X, DEF_3542 bla bla DEF_0833 blla

and what I would like

id, specificText

A1A, DEF_2122
A1A, DEF_87653
A2A, DEF_7654
A2A, DEF_2199
X1X, DEF_3542
X1X, DEF_0833
....

>Solution :

You are very close. Follow that with an explode:

data["specificText"] = data["specificText"].str.findall('([A-Z]+_\d+)')
data = data.explode('specificText')

Output:

    id specificText
0  A1A     DEF_2122
0  A1A    DEF_87653
1  A2A     DEF_7654
1  A2A     DEF_2199
2  X1X     DEF_3542
2  X1X     DEF_0833
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