The Python Oracle

Filling rows with conditions in Pandas

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Future Grid Looping

--

Chapters
00:00 Question
00:58 Accepted answer (Score 4)
01:33 Thank you

--

Full question
https://stackoverflow.com/questions/5769...

Accepted answer links:
[Series.where]: http://pandas.pydata.org/pandas-docs/sta...
[Series.str.extract]: http://pandas.pydata.org/pandas-docs/sta...

--

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()