Search code examples
pandasslicerowsdrop

Pandas Select Rows Start to End with Specific Starting Value in Column


this is something simple but I have not found an answer. I have a dataframe 'df1' that looks like this:

    year      5
0   1948 -99.99
1   1949 -99.99
2   1950  -1.74
3   1951   0.17
4   1952  -0.93

And, I simply need to select rows beginning with the 'year' column == 1950 and then to the end of the rows (which in this case is '1952').

I have tried this below but I just get "False", "True" but I just get a "True" when 'yearstart' = 1950. So, if 'yearstart' == 1950, i get the following. I'm not sure how to execute a yearstart and extract all the data to the end of the rows.

yearstart = 1950

df2 = [df1.year==yearstart]

I get this -

[0     False
 1     False
 2      True
 3     False
 4     False

What I need is this:

df2 = 

    year      5
0   1950  -1.74
1   1951   0.17
2   1952  -0.93

Solution

  • Try with idxmax and iloc:

    >>> df.iloc[df['year'].eq(1950).idxmax():]
       year     5
    2  1950 -1.74
    3  1951  0.17
    4  1952 -0.93
    >>>