2

I want to create a column, based on values in another column. I found this approach, but I don't think that this is going to work, since I need to check all the 'Unique_String' values, before 'abstracting' the data.

What do I want?

I want to 'loop'(?) through my 'Text' column, see if there is data available. If not, it should look into the 'Unique_String' column, and abstract (if available) the values, and paste it into the Text column.

Data

I have a dataframe like this:

Unique_String                 Text 
AAA                           Here is text! 
AAA                           nan
BBB                           nan
BBB                           Here is text as well! 
BBB                           Feyenoord
CCC                           nan
CCC                           nan

The desired output is:

Unique_String                 Text 
AAA                           Here is text! 
AAA                           Here is text!
BBB                           Here is text as well!
BBB                           Here is text as well! 
BBB                           Feyenoord
CCC                           nan
CCC                           nan 

Many Thanks!

R overflow
  • 1,292
  • 2
  • 17
  • 37

1 Answers1

2

Here is necessary call functions forward and back filling per groups:

df['Text'] = df.groupby('Unique_String')['Text'].apply(lambda x: x.ffill().bfill())
print (df)
  Unique_String                   Text
0           AAA          Here is text!
1           AAA          Here is text!
2           BBB  Here is text as well!
3           BBB  Here is text as well!
4           BBB              Feyenoord
5           CCC                    NaN
6           CCC                    NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252