如何从带有条件的数据框中绘制图形?

时间:2020-02-14 12:45:38

标签: python dataframe plot stock

我有一些选择权的数据,我想根据看涨期权或看跌期权的条件绘制数据。以下数据如下所示:

            underlying_last  exchange  ...   IVBid   IVAsk
underlying                             ...                
BP                    39.00         *  ...  0.0000  4.0743
BP                    39.00         *  ...  0.0000  3.0846
BP                    39.00         *  ...  0.0000  3.9024
BP                    39.00         *  ...  0.0000  2.6644
BP                    39.00         *  ...  0.0000  3.3525

有一列'type',其中包含该期权是看涨期权还是看跌期权。我只想选择看涨期权价格,然后将报价日期与价格作图。我有以下代码:

#import modules needed
import numpy as np
import pandas_datareader as web
import matplotlib.pyplot as plt
import datetime as dt
import pandas as pd
from matplotlib import style
from matplotlib import rc
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import GPy
from numpy.random import randn

#styel of plot
style.use('default')


df = pd.read_csv('BP_2019.csv',index_col = 'underlying',parse_dates = ['expiration','quotedate'])
c = df['type'] = "call"
x = df[c].index
y = df[c]['bid'].tolist()

fig, ax = plt.subplots()
ax.plot(x,y)

我收到以下错误:

  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'call'

2 个答案:

答案 0 :(得分:0)

尝试以下操作:

代替c = df['type'] = "call"

使用c = df[df['type'] == "call"]

答案 1 :(得分:0)

为您的“绘制报价日期与价格的关系”以得到x,y,请尝试:

shmctl