1

I'm trying to generate a DataFrame with the following list of column names:

cols = [
    'name',
    'age',
    'team',
    'column1',
    'column2',
    'column3',
    'column4',
    'column5',
    'column6',
]

rows = [Row(**{k: '1776-07-04'}) for k in cols]
df = spark.createDataFrame(rows)

If I run df.columns, the columns from the list above are returned as expected. But when I run df.show, I get the following error:

Caused by: java.lang.IllegalStateException: Input row doesn't have expected number of values required by the schema. 9 fields are required while 1 values are provided.

And so ultimatley I (somewhat) understand why i'm getting this error, but I was under the impression that 1776-07-04 would just be assigned to any/all values. What am I missing here?

1 Answer 1

2

This is because you are creating a Dataframe with 9 Rows but each row has only data for one of the columns.

the right way to create a single row with all 9 columns assigned a value of '1776-07-04' is

>>> df = spark.createDataFrame([Row(**{k:'1776-07-04' for k in cols})], cols)
>>> df.show()
+----------+----------+----------+----------+----------+----------+----------+----------+----------+
|      name|       age|      team|   column1|   column2|   column3|   column4|   column5|   column6|
+----------+----------+----------+----------+----------+----------+----------+----------+----------+
|1776-07-04|1776-07-04|1776-07-04|1776-07-04|1776-07-04|1776-07-04|1776-07-04|1776-07-04|1776-07-04|
+----------+----------+----------+----------+----------+----------+----------+----------+----------+
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.