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

合并两个熊猫数据框会导致“重复”列

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

合并两个熊猫数据框会导致“重复”列

之所以要添加具有后缀’_x’和’_y’的其他列,是因为要合并的列没有匹配的值,因此此冲突会产生其他列。在这种情况下,您需要删除其他“ _y”列并重命名“
_x”列:

In [145]:# define our drop functiondef drop_y(df):    # list comprehension of the cols that end with '_y'    to_drop = [x for x in df if x.endswith('_y')]    df.drop(to_drop, axis=1, inplace=True)drop_y(merged)mergedOut[145]:    key  dept_name_x  res_name_x   year_x   need   holding    DeptA_ResA_2015        DeptA        ResA     2015      1         1   1  DeptA_ResA_2016        DeptA        ResA     2016      1         1   2  DeptA_ResA_2017        DeptA        ResA     2017      1         1    no_of_inv   inv_cost_wo_ice  011000000  10      0  20      0  In [146]:# func to rename '_x' colsdef rename_x(df):    for col in df:        if col.endswith('_x'): df.rename(columns={col:col.rstrip('_x')}, inplace=True)rename_x(merged)mergedOut[146]:    key  dept_name  res_name   year   need   holding   no_of_inv    DeptA_ResA_2015      DeptA      ResA   2015      1         11   1  DeptA_ResA_2016      DeptA      ResA   2016      1         10   2  DeptA_ResA_2017      DeptA      ResA   2017      1         10    inv_cost_wo_ice  01000000  1      0  2      0

编辑 如果将公用列添加到合并中,则除非这些列上的匹配项不匹配,否则不应产生重复的列:

merge_df = pd.merge(holding_df, invest_df, on=['key', 'dept_name', 'res_name', 'year'], how='left').fillna(0)


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

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

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