简体   繁体   English

将 Pandas Dataframe 中的行按索引替换为另一个 Dataframe 中具有相应索引的值

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

相关问题 如何在Pandas DataFRame中替换列和行的索引 - how to replace index of columns and rows in Pandas DataFRame 使用列表中相应索引上的值将pandas数据框上的值映射 - Map the values on pandas dataframe using the values on the corresponding index in a list Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 用另一个 DataFrame 替换 pandas 多索引 DataFrame 的列 - Replace column of pandas multi-index DataFrame with another DataFrame 使用来自另一个 Dataframe 的索引替换 Dataframe 中的行 - Replace rows in Dataframe using index from another Dataframe 如何在大 Pandas DataFrame 中找到“True”值对应的索引和列? - How to find the `True` values' corresponding index and column in a large Pandas DataFrame? python - 使用具有相同索引的另一个 dataframe 替换 dataframe 中的值 - python - replace values in dataframe using another dataframe with same index 使用另一个数据帧替换数据帧值,无需索引匹配 - replace dataframe values using another dataframe, without index matching 在 pandas DataFrame 中找不到对应日期的索引 - Cannot find index of corresponding date in pandas DataFrame 用列表中的值替换 pandas dataframe 中的索引值 - replace index values in pandas dataframe with values from list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM