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
CodePudding user response:
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)]
CodePudding user response:
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
CodePudding user response:
This can be done using DataFrame.query()
:
df = df.query(' and '.join([f'{col}!=""' for col in ['col_1','col_2','col_3']]))