[英]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.