-1

I have a table like so

id  col_1 col_2 col_3
101 1     17    12
102       17    
103             4
    2 

how do i only records where col_1, col_2, and col_3 are not blank?

Expected output:

id  col_1 col_2 col_3
101 1     17    12
Eisen
  • 1,697
  • 9
  • 27
  • 1
    For *blank* data, you should include the output of `df.to_dict()` rather than the print out. – Quang Hoang Sep 01 '22 at 19:40
  • In case some of your "blank" data actualy consists of spaces, [see this answer](https://stackoverflow.com/a/21942746/14627505). From it, you can use `df.replace(r'^\s*$', '', regex=True)` – Vladimir Fokow Sep 01 '22 at 19:46

3 Answers3

0

This will select only those rows in the dataframe, where all ['col_1', 'col_2', 'col_3'] are non-empty:

df[df[['col_1', 'col_2', 'col_3']].ne('').all(axis=1)]
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27
0

here is one way to do it make a list of the blank, nulls etc and then convert the columns that has any of these values into a True/False, and take their sum. you need the rows where sum is zero

df[df.isin ([' ','', np.nan]).astype(int).sum(axis=1).eq(0)]

id  col_1   col_2   col_3
0   101     1   17  12
Naveed
  • 11,495
  • 2
  • 14
  • 21
0

This can be done using DataFrame.query():

df = df.query(' and '.join([f'{col}!=""' for col in ['col_1','col_2','col_3']]))

Alternatively you can do it this way:

df = df[lambda x: (x.drop(columns='id') != "").all(axis=1)]
constantstranger
  • 9,176
  • 2
  • 5
  • 19