Pandas create new date rows and forward fill column values

前端 未结 2 1114
不知归路
不知归路 2021-01-14 17:02

I have a dataframe like this:

id     date       value
 1  12/01/2016      5 
 1  25/02/2016      7 
 1  10/03/2017      13 
 2  02/04/2016      0 
 2  06/07/         


        
2条回答
  •  自闭症患者
    2021-01-14 18:03

    Consider a groupby and merge approach:

    import pandas as pd
    from io import StringIO
    from datetime import date
    
    txt= """
    id     date       value
     1  12/01/2016      5 
     1  25/02/2016      7 
     1  10/03/2017      13 
     2  02/04/2016      0 
     2  06/07/2016      1 
     2  18/04/2017      6 
    """
    
    df = pd.read_table(StringIO(txt), sep="\s+", parse_dates=[1], dayfirst=True)
    
    def expand_dates(ser):
        return pd.DataFrame({'date': pd.date_range(ser['date'].min(), date.today(), freq='D')})
    
    newdf = df.groupby(['id']).apply(expand_dates).reset_index()\
              .merge(df, how='left')[['id', 'date', 'value']].ffill()
    

提交回复
热议问题