[英]Replace column values between two dataframes according to index
I have a dataframe named calls, each call is recorded by a date (calls)[datetime].我有一个名为调用的数据框,每个调用都由日期(调用)[日期时间]记录。 each call has an answer status (calls)[Status].
每个呼叫都有一个应答状态(呼叫)[状态]。
a = {'datetime':['2021-11-22 18:47:02','2021-01-08 14:18:11','2021-06-08 16:40:45','2021-12-03 10:21:31','2021-12-03 15:21:31'],
'Status': ['P_TR_SOK', 'RS_GEN', 'P_TR_ST_','RS_GEN', 'MLR_ST']}
calls = pd.DataFrame(data = a)
A second Dataframe named NewStatus with same column ( NewStatus
)[datetime] and the column ( NewStatus
)[New_Status] that I want to replace in the first dataframe with a date join第二个名为 NewStatus 的数据框具有相同的列 (
NewStatus
)[datetime] 和列 ( NewStatus
)[New_Status] 我想在第一个数据框中用日期连接替换
b = {'datetime': ['2021-11-22 18:47:02','2021-01-08 14:18:11','2021-06-08 16:40:45','2021-12-03 10:21:31'],
'New_Status': ['AB_CL_NO','REP','AB_AUT','DROP']}
NewStatus = pd.DataFrame(data = b)
Desired result is the following for calls :调用的期望结果如下:
datetime![]() |
Status![]() |
---|---|
2021-11-22 18:47:02 ![]() |
AB_CL_NO ![]() |
2021-01-08 14:18:11 ![]() |
REP![]() |
2021-06-08 16:40:45 ![]() |
AB_AUT ![]() |
2021-12-03 10:21:31 ![]() |
DROP![]() |
2021-12-03 15:21:31 ![]() |
MLR_ST ![]() |
By using通过使用
calls.reset_index().set_index('datetime').join(NewStatus.drop_duplicates().set_index('datetime'), how='left', rsuffix='df2')
I am blocked how to replace the old Status by making the join with "datetime"我被阻止如何通过使用“日期时间”加入来替换旧状态
You could do it like this:你可以这样做:
UPDATE Be aware that pd.update
works inplace
.更新请注意
pd.update
inplace
工作。
calls= calls.set_index('datetime')
calls.update(NewStatus.set_index('datetime').rename(columns={'New_Status': 'Status'}))
print(calls)
Status
datetime
2021-11-22 18:47:02 AB_CL_NO
2021-01-08 14:18:11 REP
2021-06-08 16:40:45 AB_AUT
2021-12-03 10:21:31 DROP
2021-12-03 15:21:31 MLR_ST
Old answer旧答案
calls= calls.set_index('datetime')
out = (calls.join(NewStatus.drop_duplicates()
.set_index('datetime')
.rename(columns={'New_Status':'Status'}), how='left', rsuffix='_updated')
.rename(columns={'Status_updated': 'Replaced_Status'})
.drop('Status', axis=1)
.dropna())
print(out)
Replaced_Status
datetime
2021-11-22 18:47:02 AB_CL_NO
2021-01-08 14:18:11 REP
2021-06-08 16:40:45 AB_AUT
2021-12-03 10:21:31 DROP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.