Home > Back-end >  How to create column based on previous value within the same group in pandas DataFrame?
How to create column based on previous value within the same group in pandas DataFrame?

Time:12-29

I have a dataframe as below and want to create a column (date_fill) by filling the value in "date" column with the previous value if the value in "name" column is the same.

Input:

  name  group date
0    a     sdf  NaN
1    a     dss  NaN
2    a     fff  2022-10-23
3    b     ggg  NaN
4    b     few  NaN
5    b     mjf  NaN
6    c     ggj  NaN
7    c     ojg  NaN
8    c     ert  NaN
9    c     dfg  2022-11-03

Expected output:

  name  group date  date_fill
0    a     sdf  NaN  2022-10-23
1    a     dss  NaN  2022-10-23
2    a     fff  2022-10-23  2022-10-23
3    b     ggg  NaN  NaN
4    b     few  NaN  NaN
5    b     mjf  NaN  NaN
6    c     ggj  NaN  2022-11-03
7    c     ojg  NaN  2022-11-03
8    c     ert  NaN  2022-11-03
9    c     dfg  2022-11-03  2022-11-03

I have tried below, but it didn't work.

def date_before(df):
    if df['name']==df['name'].shift(-1):
        val = df['date'].shift(-1)
    else:
        val = np.NaN
    return val

df['date_fill'] = df.apply(date_before, axis=1)

Thanks in advance.

CodePudding user response:

Try this:

df['date_fill'] = df.groupby('name')['date'].ffill()

ffill replaces null values with the value from previous rows, just group by name for your case

CodePudding user response:

 df = df.groupby('name')   
df['date_fill']=df['date'].ffill().shift(-1)
  • Related