简体   繁体   中英

How can I drop rows in pandas based on a condition

I'm trying to drop some rows in a pandas data frame, but I'm getting this error: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

I have a list of desired items that I want to stay in the Data Frame, so I wrote this:

#
 import sys import pandas as pd biog = sys.argv[1] df = pd.read_csv(biog, sep ='\t') desired = ['Affinity Capture-Luminescence', 'Affinity Capture-MS', 'Affinity Capture-Western', 'Co-crystal Structure', 'Far Western', 'FRET', 'PCA', 'Reconstituted Complex'] new_df = df[['OFFICIAL_SYMBOL_A','OFFICIAL_SYMBOL_B','EXPERIMENTAL_SYSTEM']] for i in desired: print(i) new_df.drop(new_df[new_df.EXPERIMENTAL_SYSTEM.= i],index, inplace = True) print(new_df)
#

it works if I place a single condition at a time, but when the for loop is inserted it doesn't work.

I didn't placed here the data because it is too large, I hope that this is enough.

thanks for the help

You can set a new df of when a column is in a list of values. No need to loop it.

new_df = new_df[new_df['EXPERIMENTAL_SYSTEM'].isin(desired)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM