Calculate specific function from the Date column and input parameter specified by user in Pandas

后端 未结 1 853
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 23:12

I have a df as shown below.

Date                t_factor     
2020-02-01             5             
2020-02-03             23              
2020-02-06                     


        
相关标签:
1条回答
  • 2021-01-21 23:26

    Here is how I will go about it:

    import pandas as pd
    from io import StringIO
    from datetime import datetime, timedelta
    import numpy as np
    
    df = pd.read_csv(StringIO("""Date                t_factor     
    2020-02-01             5             
    2020-02-03             23              
    2020-02-06             14           
    2020-02-09             23           
    2020-02-13             30            
    2020-02-20             29            
    2020-02-29             100               
    2020-03-11             38                    
    2020-03-26             70           
    2020-03-29             70 """), sep="\s+", parse_dates=[0])
    df
    
    def fun(x, start="2020-02-01", end="2020-02-06", a0=3, a1=1, a2=0):
        start = datetime.strptime(start, "%Y-%m-%d")
        end = datetime.strptime(end, "%Y-%m-%d")
        if start <= x.Date <= end:
            t2 = (x.Date - start)/np.timedelta64(1, 'D') + 1
            diff = a0 + a1*t2 + a2*(t2)**2
        else:
            diff = np.NaN
        return diff
    
    df["t1"] = df.apply(lambda x: fun(x), axis=1)
    df["t2"] = df.apply(lambda x: fun(x, "2020-02-13", "2020-02-29", 2, 0, 1), axis=1)
    df["t3"] = df.apply(lambda x: fun(x, "2020-03-11", "2020-03-29", 4, 0, 0), axis=1)
    df["t_function"] =  df["t1"].fillna(0) + df["t2"].fillna(0) + df["t3"].fillna(0)
    
    df
    

    Here is the output:

     Date   t_factor    t1  t2    t3    t_function
    0   2020-02-01  5   4.0 NaN   NaN   4.0
    1   2020-02-03  23  6.0 NaN   NaN   6.0
    2   2020-02-06  14  9.0 NaN   NaN   9.0
    3   2020-02-09  23  NaN NaN   NaN   0.0
    4   2020-02-13  30  NaN 3.0   NaN   3.0
    5   2020-02-20  29  NaN 66.0  NaN   66.0
    6   2020-02-29  100 NaN 291.0 NaN   291.0
    7   2020-03-11  38  NaN NaN   4.0   4.0
    8   2020-03-26  70  NaN NaN   4.0   4.0
    9   2020-03-29  70  NaN NaN   4.0   4.0
    
    0 讨论(0)
提交回复
热议问题