![](/img/trans.png)
[英]Dataframe add new row if the index does not exist like a dictionary without checking existence
[英]dataframe index existence checking
当我执行以下代码时,它将生成水平线(根据需要),但是仅当my_data中存在此位置(可用索引位置)时(先前已过滤/选择的my_data)。
plt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
我想写一些像:
If my_data.loc[6805] is not None:
plt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
如何检查索引位置是否可用?
1]错误异常处理
KeyError:
try-except是您要寻找的
try: mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed') except: pass
2]检查6805是否为索引[仅当6805为索引时]
您可以做的另一件事是检查6805是否作为索引存在
if 6805 in my_data.index.tolist(): mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
3]检查行数
您可以检查总行数是否大于或等于6805
if my_data.shape[0] >= 6805 : mplt.axhline(y=my_data.loc[6805], color='green', linestyle='dashed')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.