0

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

2
  • 1
    Does this answer your question? delete rows based on a condition in pandas Commented Apr 14, 2020 at 21:38
  • What is the issue, exactly? Have you done any debugging? Commented Apr 15, 2020 at 0:44

1 Answer 1

1

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)] 
Sign up to request clarification or add additional context in comments.

1 Comment

@LucasLazari, no problem! Could you mark it as the correct answer? It's the checkmark just bellow the upvote/downvote arrows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.