简体   繁体   English

使用条件列从另一个框架更新 Pandas dataframe 列

[英]Update Pandas dataframe column from another frame using a conditional column

I have a main dataframe that I want to update periodically with an update frame.我有一个主要的 dataframe 我想使用更新框架定期更新。 The main frame has a column that determines which column in the update column to update from.主框架有一个列,用于确定更新列中的哪一列进行更新。 Currently, I'm able to do it as follows:目前,我可以这样做:

import pandas as pd
import numpy as np

##### Test data
# Not unique Name but still index
df_main = pd.DataFrame({
        "Name": ["a", "b", "c", "b", "d"],
        "Flip": [True, True, False, False, True],
        "Value": [1.0, 2.0, 3.0, 2.5, 4.0]
    }, columns=["Name", "Flip", "Value"])
df_main.set_index('Name', inplace=True)

#        Flip  Value
# Name              
# a      True    1.0
# b      True    2.0
# c     False    3.0
# b     False    2.5
# d      True    4.0

# Unique index
df_update_data = pd.DataFrame({
        "Name": ["a", "b", "c", "d", "f"],
        "Value_True":  [1.1, 2.1, 3.1, 4.1, 5.1],
        "Value_False": [1.2, 2.2, 3.2, 4.2, 5.2]
    }, columns=["Name", "Value_True", "Value_False"])
df_update_data.set_index('Name', inplace=True)

#       Value_True  Value_False
# Name                         
# a            1.1          1.2
# b            2.1          2.2
# c            3.1          3.2
# d            4.1          4.2
# f            5.1          5.2

df_main = df_main.join(df_update_data, how='inner')
df_main["Value"] = np.where(df_main['Flip'].values, df_main['Value_True'].values, df_main['Value_False'].values)
df_main = df_main.drop(['Value_True', 'Value_False'], axis=1)

print(df_main)
#        Flip  Value
# Name              
# a      True    1.1
# b      True    2.1
# b     False    2.2
# c     False    3.2
# d      True    4.1

This is done quite often and I actually have Name_{1,2,3}, Flop_{1,2,3}, Value_{1,2,3} so I'm doing the join, update and drop 3 times.这经常发生,我实际上有 Name_{1,2,3}, Flop_{1,2,3}, Value_{1,2,3} 所以我做了 3 次加入、更新和删除。 I'm trying to be as efficient as possible as I'm chasing time.我正在努力提高效率,因为我在追逐时间。 Is this the best way to do it?这是最好的方法吗? I did not really find a speed improvement using merge rather than join.我并没有真正发现使用合并而不是加入来提高速度。

Note that your result is sorted on the index, so my solution starts from explicit sorting (on the index).请注意,您的结果是按索引排序的,因此我的解决方案从显式排序(在索引上)开始。

I think, creation of an intermediate DataFrame is unavoidable.我认为,创建中间 DataFrame 是不可避免的。 But then you can compute values for Value column and save them just in this column.但是您可以计算Value列的值并将它们保存在此列中。

I also noticed that how='left' (default) works a bit faster and in your case is also acceptable.我还注意到how='left' (默认)工作得更快一些,在你的情况下也是可以接受的。

So the code can be:所以代码可以是:

df_main.sort_index(inplace=True)
wrk = df_main.join(df_update_data)
df_main.Value = np.where(wrk.Flip, wrk.Value_True, wrk.Value_False)

At least you avoid dropping 2 columns.至少您避免删除 2 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas数据帧条件列更新 - Pandas dataframe conditional column update 如何根据 pandas 中的条件匹配从另一个数据帧更新数据帧列值 - How to update the data frame column values from another data frame based a conditional match in pandas 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame 使用来自另一列的条件值将新列添加到Pandas数据框 - Add new column to Pandas dataframe using conditional values from another column pandas dataframe 从另一个 dataframe 有条件更新 - Conditional update of pandas dataframe from another dataframe 对熊猫数据框中的日期列进行条件更新 - Conditional update to a date column in pandas dataframe 当来自熊猫中另一个数据框的键匹配时更新数据框的列 - Update column of a dataframe when key matches from another dataframe in pandas 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 如何使用来自另一个 dataframe 的新列更新 Pandas dataframe? - How to update Pandas dataframe with a new column using values from another dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM