简体   繁体   中英

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
....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM