从数组创建 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)

你会有一些可以解释的东西。