简体   繁体   中英

How to plot a graph from a dataframe with a condition?

I have some data of some options where I want to plot the data yet with the conditions of either a call or a put. The following data looks like this:

            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

There is a column 'type' which contains if the option is a call or a put. I want to select only call values and plot the quote date against the price. I have the following code:

#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)

I am receiving the following error:

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

KeyError: 'call'

Try following:

Instead of c = df['type'] = "call"

Use c = df[df['type'] == "call"]

to get x, y for your " plot the quote date against the price" try:

x = df['date'][df.type == 'call']
y = df['bid'][df.type == 'call']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM