【发布时间】:2020-12-01 22:00:14
【问题描述】:
如果某些条件成立,我想用来自另一个数据帧 df_new 的值更新数据帧 df。
数据框的索引和列名不匹配。怎么可能?
names = ['a', 'b', 'c']
df = pd.DataFrame({
'val': [10, 10, 10],
}, index=names)
new_names = ['a', 'c', 'd']
df_new = pd.DataFrame({
'profile': [5, 15, 22],
}, index=new_names)
above_max = df_new['profile'] >= 7
# This works only if indexes of df and df_new match
#df.loc[above_max, 'val'] = df_new['profile']
# expected df:
# val
# a 10
# b 10
# c 15
【问题讨论】: