1

I have the following DF:

df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', 
                                        '11/11/2011', '11/12/2011', '','','','',''], 
                'Event' : ['Dance', 'Painting', 'Bowling', 'Rugby', 'Running','Dance', 'Painting', 'Bowling', 'Rugby', 'Running']})

df

I want to copy the value of "Date" that corresponds to the row of the specific "Event" value (from a non-blank to a blank location), if the "Event" values match. So, if 'Dance' is associated with the value '11/8/2011', I want all "Dance" to have those values under date, and not be blank.

I want the final DF to look like this:

    df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', 
                                        '11/11/2011', '11/12/2011', '11/8/2011', '11/9/2011', '11/10/2011','11/11/2011', '11/12/2011'], 
                'Event' : ['Dance', 'Painting', 'Bowling', 'Rugby', 'Running','Dance', 'Painting', 'Bowling', 'Rugby', 'Running']})

I tried:

DF_New = df.groupby(['Event'])

But get the following message, instead of a new dataframe:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7fe560cc17d0>

1 Answer 1

2

You can do groupby().ffill():

df['Date'] = (df.Date.where(df.Date.ne(''))
   .groupby(df.Event)
   .ffill()
)

Output:

         Date     Event
0   11/8/2011     Dance
1   11/9/2011  Painting
2  11/10/2011   Bowling
3  11/11/2011     Rugby
4  11/12/2011   Running
5   11/8/2011     Dance
6   11/9/2011  Painting
7  11/10/2011   Bowling
8  11/11/2011     Rugby
9  11/12/2011   Running
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.