Skip to content Skip to sidebar Skip to footer

Get Rows Between Two Values Of A Column Using Python

Suppose there is a data frame as follows: df = { 'Period': [1996,'Jan','Feb','March',1997,'Jan','Feb','March',1998,'Jan','Feb','March'] 'Some-Values': [,'a','b','c',,'d','e','f',,'

Solution 1:

Try to change your dataframe into "correct" way , then we can getting the information by using year information

df['Year']=df.loc[df['Some-Values']=='','Period']
df.Year=df.Year.ffill()
df=df.loc[df.Period!=df.Year,:]
df.loc[df.Year==1996,:]
Out[651]: 
  PeriodSome-ValuesYear1    Jan           a  19962    Feb           b  19963  March           c  1996

Solution 2:

One way via pd.Series.idxmax and pd.DataFrame.iloc:

df = pd.DataFrame({'Period': [1996,'Jan','Feb','March',1997,'Jan','Feb',
                              'March',1998,'Jan','Feb','March'],
                   'Some-Values': ['','a','b','c','','d','e','f','','g','h','i']})

res = df.iloc[(df['Period'] == 1996).idxmax()+1:(df['Period'] == 1997).idxmax()]

print(res)

  Period Some-Values
1    Jan           a
2    Feb           b
3  March           c

For readability, you can use a slice object:

slicer = slice((df['Period'] == 1996).idxmax()+1,
               (df['Period'] == 1997).idxmax())

res = df.iloc[slicer]

Post a Comment for "Get Rows Between Two Values Of A Column Using Python"