gwydion93
gwydion93

Reputation: 1923

Finding the first row index value where a condition occurs

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?

Upvotes: 0

Views: 208

Answers (1)

Ukrainian-serge
Ukrainian-serge

Reputation: 854

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)

Upvotes: 1

Related Questions