Lucas Lazari
Lucas Lazari

Reputation: 119

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

Upvotes: 0

Views: 88

Answers (1)

mrbTT
mrbTT

Reputation: 1409

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)]

Upvotes: 1

Related Questions