1
ListOfNames = ['AA', 'BA', CF']

df:

ID Name Gene Chr Info
1  DD   S     3   441
2  AA   S     5   444
3  DD   F     3   744
4  DD   VV    1   448
5  BA   S     3   445

I need to delete all rows from Name same like in a list and move it to another df.

I do something like that:

newDF= pd.DataFrame()
for i in range(len(ListOfNames )):
    a = df.where(df.Name == ListOfNames[i]).dropna()
    newDF= newDF.append(a[0:])

In old df should stay all rows without deleted. Deleted should be move to new df. My code return empty df and I don't know why.

If I run:

a = df.where(df.Name == ListOfNames[1]).dropna()

I got normally row from df with AA name.

Why it's doesn't works?

martin
  • 1,145
  • 1
  • 7
  • 24
  • 2
    Isn't this simply `df[df['Name'].isin(ListOfNames)]`? – Alexander Jul 31 '19 at 06:30
  • 1
    Possible duplicate of [Select rows from a DataFrame based on values in a column in pandas](https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas) – razdi Jul 31 '19 at 06:31

1 Answers1

3

Use the isin function:

df = df[~df.Name.isin(ListOfNames)]
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • 2
    Damn, so simple... Thank u very much. I need to more practice with pandas to not `for`ed everything :D – martin Jul 31 '19 at 06:33