3

I am starting to learn Pandas. I have seen a lot of questions here in SO where people ask how to delete a row if a column matches certain value.

In my case it is the opposite. Imagine having this dataframe:

Dataframe

Where you want to know is, if any column has in any of its row the value salty, that column should be deleted, having as a result:

Dataframe 2

I have tried with several similarities to this:

if df.loc[df['A'] == 'salty']:
   df.drop(df.columns[0], axis=1, inplace=True)

But I am quite lost at finding documentation onto how to delete columns based on a row value of that column. That code is a mix of finding a specific column and deleting always the first column (as my idea was to search the value of a row in that column, in ALL columns in a for loop.

cs95
  • 274,032
  • 76
  • 480
  • 537
M.K
  • 1,451
  • 2
  • 20
  • 34

3 Answers3

5

Perform a comparison across your values, then use DataFrame.any to get a mask to index:

df.loc[:, ~(df == 'Salty').any()]

If you insist on using drop, this is how you need to do it. Pass a list of indices:

df.drop(columns=df.columns[(df == 'Salty').any()])

df = pd.DataFrame({
    'A': ['Mountain', 'Salty'], 'B': ['Lake', 'Hotty'], 'C': ['River', 'Coldy']})
df
          A      B      C
0  Mountain   Lake  River
1     Salty  Hotty  Coldy

(df == 'Salty').any()
A     True
B    False
C    False
dtype: bool

df.loc[:, ~(df == 'Salty').any()]
       B      C
0   Lake  River
1  Hotty  Coldy

df.columns[(df == 'Salty').any()]
# Index(['A'], dtype='object')

df.drop(columns=df.columns[(df == 'Salty').any()])
       B      C
0   Lake  River
1  Hotty  Coldy
cs95
  • 274,032
  • 76
  • 480
  • 537
  • 1
    This: `df.loc[:, ~(df == 'Salty').any()]`worked just fine! Thank you! – M.K Jun 25 '19 at 13:19
  • @cs95 What if I wanted to check for a particular substring instead of a specific string? – Mega_Noob May 19 '20 at 18:59
  • 2
    @Mega_Noob You might find my answer on substring matching useful: https://stackoverflow.com/questions/11350770/select-by-partial-string-from-a-pandas-dataframe/55335207#55335207 – cs95 May 19 '20 at 20:15
1

Here's one possibility:

df = df.drop([col for col in df.columns if df[col].eq('Salty').any()], axis=1)
bexi
  • 1,066
  • 3
  • 9
0

The following is locating the indices where your desired column matches a specific value and then drops them. I think this is probably the more straightforward way of accomplishing this:

df.drop(df.loc[df['Your column name here'] == 'Match value'].index, inplace=True)

grumpyTofu
  • 181
  • 1
  • 3