Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add an 'if' statement in a dataframe so that I check there are rows so I don't run into an indexing error?

So I have the following pandas dataframe query:

has_primary_reopen = market_info.loc[before_primary_close, "market_state"].iloc[-1] == 'CTS'

For the above, I am getting an indexing error, because

market_info.loc[before_primary_close, "market_state"]

returns an empty dataframe.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I want to add an if statement to check that if the above returns an empty dataframe, then don’t execute the top query.

Is this possible?

>Solution :

Yes, you can do it like this:

if not market_info.loc[before_primary_close, "market_state"].empty:
    has_primary_reopen = market_info.loc[before_primary_close, "market_state"].iloc[-1] == 'CTS'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading