Python 如何根据下一个数据帧中的更改更新数据帧值 df1= 截止日期更新日期A b 0 05/11/2021 05/31/2021 05/08/2020 0.6 0.9 1 05/11/2021 05/31/2021 05/09/2020 0.8 0.4 2 05/12/2021 05/31/2021 05/10/2020 0.4 0.6 3 05/12/2021 05/31/2021 05/11/2020 0.5 0.4 df2= 截止日期更新日期A b 0 05/11/2021 05/11/2021 05/08/2021 1.6 0.9 1 05/11/2021 05/11/2021 05/09/2021 0.8 1.4 2 05/12/2021 05/11/2021 05/10/2021 1.4 0.6
如果df2的A列和B列的值发生更改,则值行到df1行的更改将基于df2更新列结束日期 输出Python 如何根据下一个数据帧中的更改更新数据帧值 df1= 截止日期更新日期A b 0 05/11/2021 05/31/2021 05/08/2020 0.6 0.9 1 05/11/2021 05/31/2021 05/09/2020 0.8 0.4 2 05/12/2021 05/31/2021 05/10/2020 0.4 0.6 3 05/12/2021 05/31/2021 05/11/2020 0.5 0.4 df2= 截止日期更新日期A b 0 05/11/2021 05/11/2021 05/08/2021 1.6 0.9 1 05/11/2021 05/11/2021 05/09/2021 0.8 1.4 2 05/12/2021 05/11/2021 05/10/2021 1.4 0.6,python,pandas,dataframe,Python,Pandas,Dataframe,如果df2的A列和B列的值发生更改,则值行到df1行的更改将基于df2更新列结束日期 输出 as_date end_date pdate A b 0 05/11/2021 05/08/2021 05/08/2020 0.6 0.9 1 05/11/2021 05/09/2021 05/09/2020 0.8 0.4 2 05/12/2021 05/10/2021 05/10/2020 0.4 0.6 3 05/12
as_date end_date pdate A b
0 05/11/2021 05/08/2021 05/08/2020 0.6 0.9
1 05/11/2021 05/09/2021 05/09/2020 0.8 0.4
2 05/12/2021 05/10/2021 05/10/2020 0.4 0.6
3 05/12/2021 05/31/2021 05/11/2020 0.5 0.4
代码
df2.columns += '_x' # To make it easier to concat
df1 = pd.concat([df1, df2], axis=1)
df1['end_date'] = np.where(df1.pdate_x.isna(),df1.end_date,df1.pdate_x)
df1 = df1.dropna(axis=1)
## Renaming columns of df2 as before
df2.columns = df2.columns.str.replace('_x','', regex=True)
df1
输出
as_date end_date pdate A b
0 05/11/2021 05/08/2021 05/08/2020 0.6 0.9
1 05/11/2021 05/09/2021 05/09/2020 0.8 0.4
2 05/12/2021 05/10/2021 05/10/2020 0.4 0.6
3 05/12/2021 05/31/2021 05/11/2020 0.5 0.4
解释
我们正在
连接df1和df2
,然后使用np将df1的结束日期替换为df2的更新日期。其中
感谢逻辑