1

I have to get rid of columns that don't add information to my dataset, i.e. columns with the same values in all the entries.

I devised two ways of doing this

  1. A way using max and min values:
for col in df.columns:
    if df.agg(F.min(col)).collect()[0][0] == df.agg(F.max(col)).collect()[0][0]:
        df = df.drop(col)
  1. Another one, using distinct and count:
for col in df.columns:
    if df.select(col).distinct().count() == 1:
        df = df.drop(col)

Is there a better, faster or more straight forward way to do this?

Zaka Elab
  • 556
  • 4
  • 12

2 Answers2

3
df = df.drop(*(col for col in df.columns if df.select(col).distinct().count() == 1))
0

I prefer to use the subtract method.

df1 = # Sample DataFrame #1
df2 = # Sample DataFrame #2

assert 0 == df1.subtract(df2).count()
assert 0 == df2.subtract(df1).count()

Another way is to check the union.

assert df1.count() == df1.union(df2).count()
assert df2.count() == df1.union(df2).count()
Dan Ciborowski - MSFT
  • 6,432
  • 8
  • 50
  • 82