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

有效地将一列中的值替换为另一列Pandas DataFrame

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

有效地将一列中的值替换为另一列Pandas DataFrame

使用起来

np.where
更快。使用与您使用类似的模式
replace

df['col1'] = np.where(df['col1'] == 0, df['col2'], df['col1'])df['col1'] = np.where(df['col1'] == 0, df['col3'], df['col1'])

但是,使用嵌套

np.where
稍微快一点:

df['col1'] = np.where(df['col1'] == 0, np.where(df['col2'] == 0, df['col3'], df['col2']),df['col1'])

时机

使用以下设置来产生更大的示例Dataframe和计时函数:

df = pd.concat([df]*10**4, ignore_index=True)def root_nested(df):    df['col1'] = np.where(df['col1'] == 0, np.where(df['col2'] == 0, df['col3'], df['col2']), df['col1'])    return dfdef root_split(df):    df['col1'] = np.where(df['col1'] == 0, df['col2'], df['col1'])    df['col1'] = np.where(df['col1'] == 0, df['col3'], df['col1'])    return dfdef pir2(df):    df['col1'] = df.where(df.ne(0), np.nan).bfill(axis=1).col1.fillna(0)    return dfdef pir2_2(df):    slc = (df.values != 0).argmax(axis=1)    return df.values[np.arange(slc.shape[0]), slc]def andrew(df):    df.col1[df.col1 == 0] = df.col2    df.col1[df.col1 == 0] = df.col3    return dfdef pablo(df):    df['col1'] = df['col1'].replace(0,df['col2'])    df['col1'] = df['col1'].replace(0,df['col3'])    return df

我得到以下计时:

%timeit root_nested(df.copy())100 loops, best of 3: 2.25 ms per loop%timeit root_split(df.copy())100 loops, best of 3: 2.62 ms per loop%timeit pir2(df.copy())100 loops, best of 3: 6.25 ms per loop%timeit pir2_2(df.copy())1 loop, best of 3: 2.4 ms per loop%timeit andrew(df.copy())100 loops, best of 3: 8.55 ms per loop

我尝试计时您的方法,但是它已经运行了几分钟,但没有完成。作为比较,仅对6行示例Dataframe(而不是上面测试的较大行)计时您的方法就花费了12.8
ms。



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

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

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