简体   繁体   中英

How to create column based on previous value within the same group in pandas DataFrame?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM