pythonpandasmodin

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

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