Create new column in data frame by interpolating other column in between a particular date range - Pandas

前端 未结 1 1964
面向向阳花
面向向阳花 2021-01-24 02:26

I have a df as shown below.

the data is like this.

     Date        y
0   2020-06-14  127
1   2020-06-15  216
2   2020-06-16  4
3   2020-06-17  90
4   2020         


        
相关标签:
1条回答
  • 2021-01-24 02:36

    With some tweaks to your function it works. np.where to create the new column, removing the = from your conditionals, and casting to int as per your expected output.

    def df_interpolate(df, start_date, end_date): 
        df["Date"] = pd.to_datetime(df["Date"])
        df['y_new'] = np.where((df['Date'] > start_date) & (df['Date'] < end_date), np.nan, df['y'])
        df['y_new'] = df['y_new'].interpolate().round().astype(int)
        return df
    
             Date    y  y_new
    0  2020-06-14  127    127
    1  2020-06-15  216    216
    2  2020-06-16    4      4
    3  2020-06-17   90     90
    4  2020-06-18   82     82
    5  2020-06-19   70     74
    6  2020-06-20   59     66
    7  2020-06-21   48     58
    8  2020-06-22   23     50
    9  2020-06-23   25     42
    10 2020-06-24   24     34
    11 2020-06-25   22     26
    12 2020-06-26   19     18
    13 2020-06-27   10     10
    14 2020-06-28   18     18
    15 2020-06-29  157    157
    16 2020-06-30   16     16
    17 2020-07-01   14     14
    18 2020-07-02  343    343
    
    0 讨论(0)
提交回复
热议问题