[英]Replace rows by index in a Pandas Dataframe with values with corresponding index in another Dataframe
I am trying to write a function that, given a number of user specified time step inputs, will overwrite the value at the given index with a value from another dataframe.我正在尝试编写一个 function,给定许多用户指定的时间步长输入,它将用另一个 dataframe 的值覆盖给定索引处的值。 For example:例如:
df1
index date skew
92 2019-09-02 0
93 2019-09-03 0
94 2019-09-04 0
95 2019-09-05 0
96 2019-09-06 0
97 2019-09-09 0
df2
index change
13 0.63
14 0.61
15 0.98
16 0.11
17 0.43
The result I am after:我追求的结果:
result_df
index date skew
92 2019-09-02 0
93 2019-09-03 0.63
94 2019-09-04 0.61
95 2019-09-05 0.98
96 2019-09-06 0.11
97 2019-09-09 0.43
Using a for loop and df1.at[-i, 'skew'] = df2.loc[-i, 'change']
使用 for 循环和df1.at[-i, 'skew'] = df2.loc[-i, 'change']
I am getting the following result:我得到以下结果:
index date skew
92 2019-09-02 0
93 2019-09-03 0
94 2019-09-04 0
95 2019-09-05 0
96 2019-09-06 0
97 2019-09-09 0
-5 NaT 0.63
-4 NaT 0.61
-3 NaT 0.98
-2 NaT 0.11
-1 NaT 0.43
my current function:我目前的 function:
num_timesteps = 5
def append_changes (df1, df2, num_timesteps):
# Reverse loop to start from index df1.iloc[-num_timsteps:]
for i in range(num_timesteps, 0, -1):
df1.at[-i:, 'filler'] = df2.loc[-i:, 'change']
return df1
I expect the row values under the skew column from index -5 (as per num_timesteps) to the end of the dataframe to be replaced with those values from the 'change' column in df2 at the same index.我希望从索引-5(根据 num_timesteps)到 dataframe 末尾的倾斜列下的行值将替换为 df2 中同一索引处的“更改”列中的那些值。
I think no loop is necessary, only use DataFrame.iloc
with positions of columns by Index.get_loc
for select ans set new values - for avoid match index values assign numpy array created by .values
:我认为没有必要循环,只使用DataFrame.iloc
列的位置按Index.get_loc
为 select 并设置新值 - 为避免匹配索引值分配.values
数组创建的值:2。
num_timesteps = 5
def append_changes (df1, df2, num_timesteps):
arr = df2.iloc[-num_timesteps:, df2.columns.get_loc('change')].values
df1.iloc[-num_timesteps:, df1.columns.get_loc('skew')] = arr
return df1
print (append_changes(df1, df2, num_timesteps))
date skew
index
92 2019-09-02 0.00
93 2019-09-03 0.63
94 2019-09-04 0.61
95 2019-09-05 0.98
96 2019-09-06 0.11
97 2019-09-09 0.43
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.