Filling rows with conditions in Pandas
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Filling Rows With Conditions In Pandas
00:50 Accepted Answer Score 3
01:13 Thank you
--
Full question
https://stackoverflow.com/questions/5769...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Filling Rows With Conditions In Pandas
00:50 Accepted Answer Score 3
01:13 Thank you
--
Full question
https://stackoverflow.com/questions/5769...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 3
Use Series.where with your mask and forward filling missing values:
df['B'] = df['A'].where(df['A'].str.contains('NBN')).ffill()
#your solution should be changed
#df['B'] = df['A'].where(df['A'].str.contains(r'^NBN \d|^NBN \d\.\d')).ffill()
print(df)
A B
0 NBN 3 NBN 3
1 test text1 NBN 3
2 test text2 NBN 3
3 NBN 3.1 NBN 3.1
4 test 1 NBN 3.1
5 test 2 NBN 3.1
Another solution with Series.str.extract and forward filling missing values:
df['B'] = df['A'].str.extract(r'^(NBN\s+\d\.\d|NBN\s+\d)', expand=False).ffill()