栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建一个函数,根据给定的参数(如pandas列表中的列表)在数据框中创建新行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建一个函数,根据给定的参数(如pandas列表中的列表)在数据框中创建新行

这是一种方法

# function to create the u_cumulativedef create_u_columns (ser, threshold_ns = 0.8):    # create a copy    arr_ns = ser.to_numpy().copy()    # array for latter insert    arr_idx = np.zeros(len(ser), dtype=int)    walkin_id = 0 #start at 0 not 1 for list indexing    for i in range(len(arr_ns)-1):        if arr_ns[i]>threshold_ns: # remove 1 - walkin arr_ns[i+1:] -= (1-walkin_no_show[walkin_id]) # increment later idx to add arr_idx[i] = walkin_id+1 walkin_id +=1    # for the last row    if arr_ns[-1]>threshold_ns:        arr_idx[-1] = walkin_id+1    #return a dataframe with both columns    return pd.Dataframe({'u_cumulative': arr_ns, 'mask_idx':arr_idx}, index=ser.index)

现在定义另一个功能

overbook_dfs

def overbook_dfs (df0, walkin_no_show, threshold_p ):    l_res = [] #for result    for th_p in threshold_p: #loop on threshold        # create a copy of original dataframe        df = df0.copy()         df[['u_cumulative','mask_idx']] = create_u_columns(df['Cumulative_no_show'],    threshold_ns=th_p)        # select the rows        df_toAdd = df.loc[df['mask_idx'].astype(bool), :].copy()        # replace the values as wanted        df_toAdd['No_Show'] = walkin_no_show[:len(df_toAdd)]        df_toAdd['B_ID'] = 'walkin'+df_toAdd['mask_idx'].astype(str)        df_toAdd['u_cumulative'] -= (1 - df_toAdd['No_Show'])        # add 0.5 to index for later sort        df_toAdd.index += 0.5         #append the result to a list        l_res.append(pd.concat([df,df_toAdd]) .sort_index() .reset_index(drop=True) .drop('mask_idx', axis=1)         )    return l_res

最后,将其与参数一起使用

# parameterswalkin_no_show = [ 0.3, 0.2, 0.1, 0.4, 0.5, 0.4, 0.2, 0.7, 0.8]threshold_p = [0.8, 0.9, 1.0, 1.1]# call your functiondf_0_8, df_0_9, df_1_0, df_1_1 = overbook_dfs(df, walkin_no_show, threshold_p)print (df_0_9)       B_ID  No_Show Session  slot_num  Cumulative_no_show  u_cumulative0         1      0.4      s1         1      0.40.41         2      0.3      s1         2      0.70.72         3      0.8      s1         3      1.51.53   walkin1      0.3      s1         3      1.50.84         4      0.3      s1         4      1.81.15   walkin2      0.2      s1         4      1.80.36         5      0.6      s1         5      2.40.97         6      0.8      s1         6      3.21.78   walkin3      0.1      s1         6      3.20.89         7      0.9      s1         7      4.11.710  walkin4      0.4      s1         7      4.11.111        8      0.4      s1         8      4.51.512  walkin5      0.5      s1         8      4.51.013        9      0.6      s1         9      5.11.614  walkin6      0.4      s1         9      5.11.0

请注意,如果列表

walkin_no_show
不够长,这将失败



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号