0

Suppose I have a data frame called county_compare_df. It looks like this:

enter image description here

I want to find the index value (CO_FIPS) of the first row where column Pop_Diff > 0.

first_value = county_compare_df.loc[county_compare_df['Pop_Diff'] > 0].iloc[0]

The above throws an IndexError: single positional indexer is out-of-bounds. How can I fix this to achieve my desired outcome (EX: '01011')?

UpDate Per the comment below, I tried this:

tt = county_compare_df.loc[county_compare_df['Pop_Diff'] == 0.0].iloc[0]
print(tt)

which returns:

Pop_SFHA_x     3.420000e+03
HU_SFHA_x      1.596140e+03
Area_SFHA_x    9.082000e+01
HUC8           1.512091e+08
Pop_SFHA_y     3.420000e+03
HU_SFHA_y      1.596140e+03
Area_SFHA_y    9.082000e+01
Pop_Diff       0.000000e+00
HU_Diff        0.000000e+00
Area_Diff      0.000000e+00
Name: 01001, dtype: float64

All I want is the Name, 01001. How do I get that value?

gwydion93
  • 1,681
  • 3
  • 28
  • 59
  • 1
    You probably have no rows where `Pop_Diff` is greater than 0. – ifly6 Feb 28 '20 at 18:47
  • first_value = county_compare_df[county_compare_df['Pop_Diff']>0].index[0] – Enrique Ortiz Casillas Feb 28 '20 at 19:08
  • That also throws `IndexError: index 0 is out of bounds for axis 0 with size 0`. I thing @ifly6 is right; maybe there are no rows where the condition is met. – gwydion93 Feb 28 '20 at 19:15
  • Then just try print(tt.name) I think you got it man – Enrique Ortiz Casillas Feb 28 '20 at 19:16
  • Also, please read [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) on how to post a proper question. Your question needs to have samples data, output data, and desired output data so that they can be copied and worked on here. Samples in yourquestion have to be simple and reproducible. – Ukrainian-serge Feb 28 '20 at 23:50
  • Please read [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) article on posting a GOOD QUESTION. You don't have sample data to work with, and an example of what you want the output to look like. – Ukrainian-serge Feb 28 '20 at 23:53

1 Answers1

1

Try this:

# will get you the index number of the first item
tt = county_compare_df.loc[county_compare_df['Pop_Diff'] == 0.0].index[0] 

print(tt)
Ukrainian-serge
  • 854
  • 7
  • 12