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

Find negative number within iterrow columns

I have a dataframe called dataframe and I want to find negative values location. The dataframe looks something like this:

label 123 456
abc 2.01 -9.7
xyz 5.73 5.65
qwe -6.0 3.33

I am wondering if there is a way to run: for index, columns in dataframe.iterrows(): and then inside of the loop, search for negative values row by row. Currently, if I run this with print(column) I get a result such as:

Name: abc
123   2.01
456   -9.7
Name: xyz
123   5.73
456   5.65
Name: qwe
123   -6.0
456   3.33

How do I search these values and get a result of the index, column of a negative number?

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

>Solution :

You could just access the appropriate index in the series:

for index, series in df.iterrows():
  if series[1] < 0:
    print(f'{index} has negative value')

Or if you want to check any of the numeric values, then:

for index, series in df.iterrows():
  if any(series[1:] < 0):
    print(f'{index} has negative value')
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