PySpark code that turns columns into rows
There is a function in the standard library to create closure for you: functools.partial. This mean you can focus on writting your function as naturally as possible and bother of binding parameters later on.

As for your function:


Don't explicitly increment a running index, use enumerate instead;
You can use zip to iterate over two iterables at the same time;
Prefer using a list-comprehension to using [] + for + append;
You can use next on an iterator to retrieve an element and advance it outside of a for loop;
Avoid wildcard imports, they clutter the namespace and may lead to name collisions.
Use an if __name__ == '__main__': guard for your top-level code.


Proposed improvements

from functools import partial

from pyspark.sql import spark, Row


def flatten_table(column_names, column_values):
    row = zip(column_names, column_values)
    _, key = next(row)  # Special casing retrieving the first column
    return [
        Row(Key=key, ColumnName=column, ColumnValue=value)
        for column, value in row
    ]


if __name__ == '__main__':
    sample = spark.read.format("csv").options(header='true', delimiter = ',').load("/FileStore/tables/sample.csv")
    sample.rdd.flatMap(partial(flatten_table, sample.columns)).toDF().show()