简体   繁体   English

根据索引替换两个数据框之间的列值

[英]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 2021-11-22 18:47:02 AB_CL_NO AB_CL_NO
2021-01-08 14:18:11 2021-01-08 14:18:11 REP代表
2021-06-08 16:40:45 2021-06-08 16:40:45 AB_AUT AB_AUT
2021-12-03 10:21:31 2021-12-03 10:21:31 DROP降低
2021-12-03 15:21:31 2021-12-03 15:21:31 MLR_ST 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.

相关问题 按索引合并两个 pandas 数据帧并替换 Python 中的列值 - Merge two pandas dataframes by index and replace column values in Python 加入两个数据帧并替换 Python 中的列值 - JOIN two DataFrames and replace Column values in Python 查找位于其他两个DataFrame的索引值之间的DataFrame的索引值 - Find index values of a DataFrame that are between the index values of two other DataFrames 如何根据行索引和列名称在两个数据帧之间复用多个值? - How to multiple values between two dataframes based on row index and column name? Pandas 替换单元格值以匹配两个数据帧之间的索引 - Pandas replace cell value for matching index between two dataframes 在两个不同的 pandas 数据帧之间搜索和替换值 - Search and replace values between two different pandas dataframes 匹配两个数据框之间的列值,如果为真,则获取列名 - Match column values between two dataframes and obtain column name if true 获取数据帧之间不匹配列值的索引号 - Get the index numbers of mismatching column values between to dataframes 添加带有两个数据框之间匹配值列表的新列 - Add new column with a list of the matching values between two dataframes 根据两个 Pandas DataFrames 之间的条件为新列赋值 - Assign values to new column based on conditions between two pandas DataFrames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM