When I execute the following code, it generates the horizontal line (as desired), but only when this location (index position available) is present in my_data (my_data filtered/chosen previously).
plt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
I would like to write something like:
If my_data.loc[6805] is not None:
plt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
How can I check if the index position is available or not?
1] Error Exception Handling
KeyError:
try-except is what you are looking for
try: mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed') except: pass
2] Check if 6805 is the index [this is only if 6805 is the index]
Other thing you can do is check in 6805 exist as the index
if 6805 in my_data.index.tolist(): mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
3] Check the number of rows
You can check if the total number of rows is greater then or equal to 6805
if my_data.shape[0] >= 6805 : mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
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.