python - Python Pandas:使用 apply 方法从另一个数据框中更新多个列值
问题描述
我想从另一个数据框中更新多列的值我使用 combine_first 找到了解决方案
'''
df1 = pd.DataFrame({'id' : ['1470', '1550', '1562', '1578', '1645', '1867', '1888', '2205', '2283', '2306'],
'gp' : ['nl_i', 'adv_i', 'adv_i', 'nl_i', 'adv_i', 'early_i', 'nl_i', 'nl_i', 'nl_i', 'nl_i'],
'cho' : [69626.0, 183425.0, 75418.0, 84239.0, 158721.0, 122857.0, 166052.0, 86686.0, 140407.0, 122792.0],
'date_i' : ['2000-11-29', '2000-11-28', '2000-11-27', '2000-11-26', '2000-11-25', '2000-11-24', '2000-11-23', '2000-11-22', '2000-11-21', '2000-11-20'],
})
df1['rl'] = np.NAN
df1['date_f'] = np.NAN
df2 = pd.DataFrame({'no' : ['2939', '2283', '1578', '2781', '2319', '2306', '1888', '1470', '2869', '2205'],
'date_i' : ['2010-09-18', '2012-02-08', '2012-04-09', '2012-04-23', '2012-05-08', '2012-09-04', '2013-08-29', '2013-09-09', '2014-02-24', '2015-11-19'],
'rl' : ['r', 'l', 'r', 'r', 'l', 'r', 'r', 'r', 'r', 'r']})
df3 = pd.DataFrame ({'no' : ['2319', '2306', '1888', '1470', '2869', '2205'],
'date_f' : ['2019-05-10', '2013-09-24', '2019-06-12', '2016-08-29', '2016-10-10', '2017-11-30']})
'''
在这里,df1 的 'id' 与 df2 和 df3 的 'no' 中的值相同 我想从 df2 和 df3 的值更新 ['date_i', 'rl', 'date_f'] 列 下一个数据帧是我想要的
我使用 combine_first 在堆栈溢出中找到了这个解决方案
'''
df1_new = df2.set_index('no').combine_first(df1.set_index('id')).reset_index()
df1_new.rename(columns = {'index':'id'}, inplace = True)
df1_new = df1_new[df1.columns]
df1_new.dropna(subset=['cho'], inplace = True)
df1_new = df3.set_index('no').combine_first(df1_new.set_index('id')).reset_index()
df1_new.rename(columns = {'index':'id'}, inplace = True)
df1_new = df1_new[df1.columns]
df1_new.dropna(subset=['cho'], inplace = True)
'''
但是,我尝试使用 apply 方法解决这个问题
我可以使用 apply 方法而不是 combine_first 来更新我的数据框吗?
解决方案
我们在不使用 apply 的情况下合并了每个数据框,替换了日期的 NaN 值并删除了不必要的列。如果你真的要使用apply函数,这个答案不会令人满意。
df1_new = df1.merge(df2, left_on='id', right_on='no', how='left')
df1_new = df1_new.merge(df3,left_on='id', right_on='no', how='left')
df1_new['date_i_y'].fillna('', inplace=True)
for idx, row in df1_new.iterrows():
if len(row['date_i_y']) == 0:
df1_new.loc[idx,'date_i_y'] = row['date_i_x']
df1_new.drop(['date_i_x','no_y'], axis=1, inplace=True)
df1_new.columns = ['id', 'gp', 'cho', 'no', 'date_i', 'rl', 'date_f']
df1_new
id gp cho no date_i rl date_f
0 1470 nl_i 69626.0 1470 2013-09-09 r 2016-08-29
1 1550 adv_i 183425.0 NaN 2000-11-28 NaN NaN
2 1562 adv_i 75418.0 NaN 2000-11-27 NaN NaN
3 1578 nl_i 84239.0 1578 2012-04-09 r NaN
4 1645 adv_i 158721.0 NaN 2000-11-25 NaN NaN
5 1867 early_i 122857.0 NaN 2000-11-24 NaN NaN
6 1888 nl_i 166052.0 1888 2013-08-29 r 2019-06-12
7 2205 nl_i 86686.0 2205 2015-11-19 r 2017-11-30
8 2283 nl_i 140407.0 2283 2012-02-08 l NaN
9 2306 nl_i 122792.0 2306 2012-09-04 r 2013-09-24
推荐阅读
- html - 我有一个关于在 django 中显示带有条件的数据和 html 元素的问题
- reactjs - 如何访问 componentDidMount 中的 props
- javascript - 如何让使用背面可见性的图像在转换后不消失?
- jenkins - 如何获取依赖于 Jenkins 中某个插件的插件?
- python - wsl 2 上的 pipenv 自定义脚本:shlex.py instream 对象没有读取属性
- node.js - 将 npm install 的输出复制到 docker 容器
- sql - 我得到 ORA-00928 试图插入新数据
- ember.js - 如何从 Ember 路由模型钩子传递 @tracked 对象
- python - Beautiful Soup:从 p 元素中分离出 span 元素
- maven - 使用 maven pom.xml 生成包含 WAR 文件的 Ear 文件