Delete all Rows Based on Certain Criteria in Python

Viewed 54

I have a dataframe that looks like this:

subject     session
  1           1:1
  1           2:1
  1           3:1
  1           3:2

What i'm looking to do is if there are instances of second attempts in a session (e.g. 3:2) then I want to delete the rows that have both the first and second attempts of that session. So in this example I want to tell python to delete both 3:1 and 3:2

3 Answers

Something like this:

In [1479]: df[['session1', 'session2']] = df.session.str.split(':', expand=True)

In [1493]: df[df.groupby(['session1'])['session2'].transform('size').eq(1)].drop(['session1', 'session2'], axis=1)
Out[1493]: 
   subject session
0        1     1:1
1        1     2:1

You can do this:

from pandas import DataFrame as df

dataframe = df(data={'A': [1, 2, 3], 'B': [3, 4, 5], 'C': [6, 7, 8 ]})
new_dataframe = dataframe[dataframe.B <= 3]
In [1]: from pandas import DataFrame as df

In [2]: dataframe = df(data={'A': [1, 2, 3], 'B': [3, 4, 5], 'C': [6, 7, 8 ]})

In [3]: dataframe
Out[4]: 
   A  B  C
0  1  3  6
1  2  4  7
2  3  5  8

In [5]: new_dataframe = dataframe[dataframe.B <= 3]

In [6]: new_dataframe
Out[7]: 
   A  B  C
0  1  3  6

Source: kite.com

You want to extract the session and remove duplicate:

df['session1'] = df['session'].str.extract('^([^:]+)' )
df.drop_duplicates(['subject','session1'], keep=False)

Output:

   subject session session1
0        1     1:1        1
1        1     2:1        2
Related