1

how can I find the columns which have a value in every row My pd.DataFrame looks like this:

     A    B    C    D
1    1    2    3    2
2    2    2         1
3         3    2    1

i would like to get this output

     B    D
1    2    2
2    2    1
3    3    1

how can i detect the right columns?

Thank you, R

raffa_sa
  • 415
  • 2
  • 4
  • 13
  • I've marked this as a duplicate of a question asking for columns containing any null value. You just need to use the negative of this, i.e. the `~` operator. – jpp Oct 12 '18 at 11:37

1 Answers1

2

Use if no values are missing values:

df1 = df.loc[:, df.notna().all()]
#oldier pandas versions
#df1 = df.loc[:, df.notnull().all()]

print (df1)
   B  D
1  2  2
2  2  1
3  3  1

Explanation:

Compare no missing values by by notna:

print (df.notna())
       A     B      C     D
1   True  True   True  True
2   True  True  False  True
3  False  True   True  True

Check if all values in columns are True by DataFrame.all:

print (df.notna().all())
A    False
B     True
C    False
D     True
dtype: bool

If no values are empty strings compare by DataFrame.ne (!=):

df = df.loc[:, df.ne('').all()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252