Shankar
Shankar

Reputation: 8957

pandas dataframe - drop remaining rows based on specific column value

I wanted to drop remaining rows on pandas dataframe when the specific column value has that value.

For example, if the col1 has value as "FEB", drop remaining all rows including that row.

Code:

df = pd.read_excel(path, usecols=range(32, 89), skiprows=9).dropna(how='all')

Upvotes: 0

Views: 164

Answers (3)

D3L
D3L

Reputation: 1

Try invert (~) operator:

df=df[~df['col1'].str.match('FEB')]

to select number, you can directly use:

new_dataframe=df[df.col1 != #put number here] 

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

you can also use cumsum:

df = df[df['col1'].eq('FEB').cumsum().eq(0)]

Test data:

df = pd.DataFrame({'col1': ['JAN', 'MAY', 'FEB', 'JAN', 'FEB'],
                   'col2': [1,2,3,4,5]}) 

  col1  col2
0  JAN     1
1  MAY     2
2  FEB     3
3  JAN     4
4  FEB     5

output:

    col1    col2
0   JAN     1
1   MAY     2

Upvotes: 1

BENY
BENY

Reputation: 323226

In your case using argmax

yourdf=df.iloc[:df.col1.eq('FEB').values.argmax(),:].copy()

Upvotes: 1

Related Questions