QuestionQ203

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

A developer has an employee.csv file with the columns ID, Name, and Salary. The developer wants to load it into a DataFrame while explicitly defining the columns as integer, string, and float types, respectively.

Which code snippet accomplishes this?

Explanation

PySpark’s DataFrameReader.csv accepts a StructType as its schema argument. A schema containing IntegerType, StringType, and FloatType for ID, Name, and Salary, respectively, explicitly enforces the required column types; setting the header option ensures the first row is treated as column names.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!