Python 基于列值删除数据帧行
我基于一个网站()创建了代码,用于根据列值删除数据框中的行。“区域类型”列可以有5个值之一(响应按钮文本、响应按钮图像、固定、时间限制屏幕或继续按钮)。除非该行的值为“response\u button\u image”,否则我希望从数据帧中删除该行Python 基于列值删除数据帧行,python,dataframe,Python,Dataframe,我基于一个网站()创建了代码,用于根据列值删除数据框中的行。“区域类型”列可以有5个值之一(响应按钮文本、响应按钮图像、固定、时间限制屏幕或继续按钮)。除非该行的值为“response\u button\u image”,否则我希望从数据帧中删除该行 # select all of the rows that are not to do with the task i.e. fixation screens etc. indexZoneType = df[ (df['zone_type'] =
# select all of the rows that are not to do with the task i.e. fixation screens etc.
indexZoneType = df[ (df['zone_type'] == 'fixation') & (df['zone_type'] == 'response_button_text') & (df['zone_type'] == 'timelimit_screen') & (df['zone_type'] == 'continue_button')].index
# delete these rows
df.drop(indexZoneType , inplace=True)
我觉得这个代码应该有用?我没有得到任何错误,但是当执行print(df)
时,数据帧没有改变
谢谢。好的,所以您的条件是相互排斥的,您可能想在那里使用“或”,即
indexZoneType=df[(df['zone_type']='fixement'))(df['zone_type']='response_button_text')(df['zone_type']='timelimit_screen')(df['zone_type']='continue_button')。索引
或者更好-使用df.isin(…)
和df.loc[…]
indexZoneType=df.loc[df['zone\u type'].isin(['fixture','response\u button\u text','timelimit\u screen','continue\u button'])。索引
或者更简单:
indexZoneType=df.index[df['zone\u type'].isin(['fixture'、'response\u button\u text'、'timelimit\u screen'、'continue\u button'])]
参考: