0

DataFrame like this:

import pandas
df = pandas.DataFrame({'id':[1,2,3,4,5,6],'name':['test1','test2','test','D','E','F'],'sex':['man','woman','woman','man','woman','man']},index=['a','b','c','d','e','f'])
print(df)
print('*'*100)

I can drop the rows by index label:

df.drop(df[df.name.str.contains('test')|df.sex.str.contains('woman')].index,inplace=True)
print(df)

How can i find out the columns label which contains 'test' or 'woman' and remove the columns

lonlec
  • 23
  • 3
  • ```df.drop(df[df.name.str.contains('test')|df.sex.str.contains('woman')],inplace=True, axis=1)``` – darth baba Aug 06 '21 at 08:41
  • @darthbaba `inplace` is not recommend anymore see [this post](https://stackoverflow.com/questions/43893457/understanding-inplace-true) – Umar.H Aug 06 '21 at 08:48
  • The result of your code shows the Empty DataFrame,but I just want to filter columns by criteria and drop the columns like column 'name' ,column 'sex' ,keep 'id' column in it – lonlec Aug 06 '21 at 09:04
  • From what I can understand you need only those IDs which doesn't have 'test' in 'name' column and 'woman' in 'sex' column? Then ```df[~(df.name.str.contains('test')) & ~(df.sex.str.contains('woman'))]['id']``` will give you such IDs – darth baba Aug 06 '21 at 09:25

1 Answers1

0

use a bitwise ampersand & for an AND condition and just re-assign the dataframe.

you can invert conditions with ~

it's recommended not to use inplace anymore see this post

df1 = df[~(df['name'].str.contains('test')
                         ) & ~(df['sex'].str.contains('woman'))]


print(df1)

   id name  sex
d   4    D  man
f   6    F  man
Umar.H
  • 22,559
  • 7
  • 39
  • 74