title: Count number of columns whose values match conditions among selected columns pyspark
body: <p>I have a pyspark dataframe that looks like the following:</p>
  <pre><code>+---------------+-------------------+-------------------------------------+
  |fields_0_method| fields_1_method  |fields_2_method
  +-------+-----------------------------------------------------------------+
  |NIGHT           |MORNING          |       NIGHT                          |
  +-------+--------------------------------------------------------------------+  +-------+-----------------------------------------------------------------+
  |NIGHT           |NIGHT         |       NIGHT                          |
  +-------+--------------------------------------------------------------------
  </code></pre>
  <p>How can I create a new column that count numbers of columns among  &quot;fields_0_method&quot;, &quot;fields_1_methods&quot; and &quot;fields_2_method&quot; that have values &quot;NIGHT&quot;?</p>
  <p>Ideal dataframe looks like the followig:</p>
  <pre><code>+---------------+-------------------+-------------------------------------+
  |fields_0_method| fields_1_method  |fields_2_method    | field_method_Night_Count
  +-------+-----------------------------------------------------------------+
  |NIGHT           |MORNING          |       NIGHT       |    2
  +-------+--------------------------------------------------------------------+   +-------+-----------------------------------------------------------------+
  |NIGHT           |NIGHT         |       NIGHT          |    3
  +-------+--------------------------------------------------------------------
  </code></pre>
  <p>update:
  I followed Lamanus's approach but got this error:</p>
  <pre><code>ParseException: u&quot;\nextraneous input '&gt;' expecting {'(', 'SELECT', 'FROM', 'ADD', 'AS', 'ALL', 'DISTINCT', 'WHERE', 'GROUP', 'BY', 'GROUPING', 'SETS', 'CUBE', 'ROLLUP', 'ORDER', 'HAVING', 'LIMIT', 'AT', 'OR', 'AND', 'IN', NOT, 'NO', 'EXISTS', 'BETWEEN', 'LIKE', RLIKE, 'IS', 'NULL', 'TRUE', 'FALSE', 'NULLS', 'ASC', 'DESC', 'FOR', 'INTERVAL', 'CASE', 'WHEN', 'THEN', 'ELSE', 'END', 'JOIN', 'CROSS', 'OUTER', 'INNER', 'LEFT', 'SEMI', 'RIGHT', 'FULL', 'NATURAL', 'ON', 'LATERAL', 'WINDOW', 'OVER', 'PARTITION', 'RANGE', 'ROWS', 'UNBOUNDED', 'PRECEDING', 'FOLLOWING', 'CURRENT', 'FIRST', 'AFTER', 'LAST', 'ROW', 'WITH', 'VALUES', 'CREATE', 'TABLE', 'DIRECTORY', 'VIEW', 'REPLACE', 'INSERT', 'DELETE', 'INTO', 'DESCRIBE', 'EXPLAIN', 'FORMAT', 'LOGICAL', 'CODEGEN', 'COST', 'CAST', 'SHOW', 'TABLES', 'COLUMNS', 'COLUMN', 'USE', 'PARTITIONS', 'FUNCTIONS', 'DROP', 'UNION', 'EXCEPT', 'MINUS', 'INTERSECT', 'TO', 'TABLESAMPLE', 'STRATIFY', 'ALTER', 'RENAME', 'ARRAY', 'MAP', 'STRUCT', 'COMMENT', 'SET', 'RESET', 'DATA', 'START', 'TRANSACTION', 'COMMIT', 'ROLLBACK', 'MACRO', 'IGNORE', 'BOTH', 'LEADING', 'TRAILING', 'IF', 'POSITION', '+', '-', '*', 'DIV', '~', 'PERCENT', 'BUCKET', 'OUT', 'OF', 'SORT', 'CLUSTER', 'DISTRIBUTE', 'OVERWRITE', 'TRANSFORM', 'REDUCE', 'SERDE', 'SERDEPROPERTIES', 'RECORDREADER', 'RECORDWRITER', 'DELIMITED', 'FIELDS', 'TERMINATED', 'COLLECTION', 'ITEMS', 'KEYS', 'ESCAPED', 'LINES', 'SEPARATED', 'FUNCTION', 'EXTENDED', 'REFRESH', 'CLEAR', 'CACHE', 'UNCACHE', 'LAZY', 'FORMATTED', 'GLOBAL', TEMPORARY, 'OPTIONS', 'UNSET', 'TBLPROPERTIES', 'DBPROPERTIES', 'BUCKETS', 'SKEWED', 'STORED', 'DIRECTORIES', 'LOCATION', 'EXCHANGE', 'ARCHIVE', 'UNARCHIVE', 'FILEFORMAT', 'TOUCH', 'COMPACT', 'CONCATENATE', 'CHANGE', 'CASCADE', 'RESTRICT', 'CLUSTERED', 'SORTED', 'PURGE', 'INPUTFORMAT', 'OUTPUTFORMAT', DATABASE, DATABASES, 'DFS', 'TRUNCATE', 'ANALYZE', 'COMPUTE', 'LIST', 'STATISTICS', 'PARTITIONED', 'EXTERNAL', 'DEFINED', 'REVOKE', 'GRANT', 'LOCK', 'UNLOCK', 'MSCK', 'REPAIR', 'RECOVER', 'EXPORT', 'IMPORT', 'LOAD', 'ROLE', 'ROLES', 'COMPACTIONS', 'PRINCIPALS', 'TRANSACTIONS', 'INDEX', 'INDEXES', 'LOCKS', 'OPTION', 'ANTI', 'LOCAL', 'INPATH', STRING, BIGINT_LITERAL, SMALLINT_LITERAL, TINYINT_LITERAL, INTEGER_VALUE, DECIMAL_VALUE, DOUBLE_LITERAL, BIGDECIMAL_LITERAL, IDENTIFIER, BACKQUOTED_IDENTIFIER}(line 1, pos 153)\n\n== SQL ==\naggregate(array(fields_shippingDetails_0_shippingMethod_x,fields_shippingDetails_1_shippingMethod,fields_shippingDetails_2_shippingMethod), 0, (acc, x) -&gt; if(x == 'DROPOFFSHIPPINGGROUP', acc + 1, acc))\n---------------------------------------------------------------------------------------------------------------------------------------------------------^^^\n&quot;
  </code></pre>