[英]Delete all Rows Based on Certain Criteria in Python
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 (eg 3:2) then I want to delete the rows that have both the first and second attempts of that session.我想要做的是,如果会话中有第二次尝试的实例(例如 3:2),那么我想删除具有该会话的第一次和第二次尝试的行。 So in this example I want to tell python to delete both 3:1 and 3:2所以在这个例子中,我想告诉 python 删除 3:1 和 3:2
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.