簡體   English   中英

從數組創建DataFrame

[英]Create a DataFrame from an array

我需要從一個已經包含數據的數組創建一個DataFrame。 我是Python的新手,不理解我是否需要迭代(無論如何都需要CALL評估)。

基本上我需要從數字數組SPREAD創建一個數據框

import numpy as np
import pandas as pd 

def call_price ( precio, strike, time, volat):
    r = 0.02  # riskless short rate
    I = 100000  # number of simulations
    z = np.random.standard_normal(I)  # pseudorandom numbers
    ST = precio * np.exp((r - 0.5 * volat ** 2) * time + volat * np.sqrt(time) * z)
    hT = np.maximum(ST - strike, 0)  # inner values at maturity
    C0 = np.exp(-r * time) * np.sum(hT) / I  # Monte Carlo estimator
    return (C0)

precios = [2300,2400,2500,2600,2700,2800]

for i in range(6):
    precio_call = call_price(precios[i],2400,0.333,0.09) 
    spread = precios[i] - precio_call
    print(spread)
    #df = pd.DataFrame(spread)   #it doesn't work

您不是在列表上而是在實際號碼上調用pd.DataFrame 相反,如果你這樣做

spreads = []
for i in range(6):
    precio_call = call_price(precios[i],2400,0.333,0.09) 
    spread = precios[i] - precio_call
    spreads.append(spread)

df = pd.DataFrame(spreads)

您將擁有可以解釋的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM