简体   繁体   English

用另一个 Dataframe 的值填充 Dataframe(不是相同的列名)

[英]Fill Dataframe with values from another Dataframe (not the same column names)

I'm trying to fill a empty dataframe (OutputData) in Python with values from another dataframe (InputData).我正在尝试用另一个 dataframe (InputData) 中的值填充 Python 中的空 dataframe (OutputData)。

InputData has four columns ("Strike", "DTE", "IV", "Pred_IV") OutputData has as an index all unique Strikes from InputData and as Column names all unique DTE from Input Data. InputData 有四列(“Strike”、“DTE”、“IV”、“Pred_IV”)OutputData 具有来自 InputData 的所有唯一 Strikes 作为索引,并且作为列名称,所有来自 Input Data 的唯一 DTE。

My goal is to fill the OutputData with the corresponding "Pred_IV" values from InputData.我的目标是用来自 InputData 的相应“Pred_IV”值填充 OutputData。 As it needs to match both the index and the column name I'm not getting my head around on how to do it with any known function.因为它需要同时匹配索引和列名,所以我没有考虑如何使用任何已知的 function 进行匹配。

If there is no value in InputData which matches both the index and column name the value can remain NaN如果 InputData 中没有与索引和列名匹配的值,则该值可以保持为 NaN

Find below the dataframes I use with the df.to_dict() extract for additional detail.在下面找到我使用 df.to_dict() 提取的数据帧以获取更多详细信息。

Many thanks for your help.非常感谢您的帮助。

Best, Flo最好的,弗洛

InputData.head()输入数据.head()

    Strike  DTE     IV      Pred_IV
8   0.5131  2.784   0.3366  0.733360
9   0.5131  3.781   0.3291  0.735295
20  0.5864  2.784   0.3178  0.733476
21  0.5864  3.781   0.3129  0.735357
22  0.5864  4.778   0.3008  0.736143

InputData.head().to_dict() InputData.head().to_dict()

{'Strike': {8: 0.5131, 9: 0.5131, 20: 0.5864, 21: 0.5864, 22: 0.5864},
 'DTE': {8: 2.784, 9: 3.781, 20: 2.784, 21: 3.781, 22: 4.778},
 'IV': {8: 0.33659999999999995,
  9: 0.32909999999999995,
  20: 0.3178,
  21: 0.3129,
  22: 0.30079999999999996},
 'Pred_IV': {8: 0.7333602770095773,
  9: 0.7352946387206533,
  20: 0.7334762408944806,
  21: 0.7353567361456718,
  22: 0.7361431377881676}})

OutputData.head()输出数据.head()

        0.025   0.101   0.197   0.274   0.523   0.772   1.769   2.267   2.784   3.781   4.778   5.774
0.5131  NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
0.5864  NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
0.6597  NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
0.7330  NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
0.7697  NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN

OutputData.head(2).to_dict() OutputData.head(2).to_dict()

{0.025: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 0.101: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 0.197: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 0.274: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 0.523: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 0.772: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 1.769: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 2.267: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 2.784: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 3.781: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 4.778: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan},
 5.774: {0.5131: nan,
  0.5864: nan,
  0.6597: nan,
  0.733: nan,
  0.7696999999999999: nan}}

Here is a way to do what I believe your question is asking:这是一种方法来做我认为你的问题是问:

import pandas as pd
import numpy as np
InputData = pd.DataFrame(
    columns='Strike,DTE,IV,Pred_IV'.split(','),
    index=[8,9,20,21,22],
    data=[[0.5131,  2.784,   0.3366,  0.733360],
    [0.5131,  3.781,   0.3291,  0.735295],
    [0.5864,  2.784,   0.3178,  0.733476],
    [0.5864,  3.781,   0.3129,  0.735357],
    [0.5864,  4.778,   0.3008,  0.736143]])

OutputData = pd.DataFrame(data=np.NaN,
    columns=pd.Index(name='DTE', data=list(set(InputData.DTE.to_list()))), 
    index=pd.Index(name='Strike', data=list(set(InputData.Strike.to_list()))))
def foo(x):
    OutputData.loc[x.Strike, x.DTE] = x.Pred_IV
InputData.apply(foo, axis=1)
print(OutputData)

Output: Output:

DTE        2.784     3.781     4.778
Strike
0.5131  0.733360  0.735295       NaN
0.5864  0.733476  0.735357  0.736143

If you prefer unnamed indexes, you can do this instead:如果你更喜欢未命名的索引,你可以这样做:

OutputData = pd.DataFrame(data=np.NaN,
    columns=list(set(InputData.DTE.to_list())),
    index=list(set(InputData.Strike.to_list())))

Output: Output:

           2.784     3.781     4.778
0.5131  0.733360  0.735295       NaN
0.5864  0.733476  0.735357  0.736143

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

相关问题 如何根据条件从数据框列名称填充列值? - How to fill column values from dataframe column names based on condition? 使用同一列中的值更新数据框中的一列 - Updating a column in a dataframe with values from the same column another dataframe 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe 如何用列名填充 dataframe 的真值? - How to fill true values of a dataframe with column names? 通过将列名与字典匹配来填充 dataframe 中的值 - Fill values in dataframe by matching column names to dictionary 从另一个数据帧填充数据帧的列 - Fill column of a dataframe from another dataframe 如何用同一列中的值填充 null 列中的 Pyspark Dataframe 值,其在另一列中的对应值相同 - How to fill null values in a Pyspark Dataframe column with values from the same column, whose corresponding value in another column is same Gapfill pandas dataframe 在另一个 dataframe 中使用同一列中的值 - Gapfill pandas dataframe using values from same column in another dataframe 如何在另一个 dataframe 的特定列名上填写 dataframe - How can I fill a dataframe on specific column names of another dataframe 用另一个 dataframe 列值中的值填充 dataframe 列 - To fill a dataframe column by values in another dataframe column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM