{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial-2: Introduction to PySpark DataFrames" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import and initialize SparkSession" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pyspark.sql import SparkSession\n", "\n", "spark = SparkSession.builder.appName(\"tutorial-2\").getOrCreate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# PySpark Dataframes\n", "\n", "## Read in a csv file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = spark.read.csv(\"./cars.csv\", header = True)\n", "\n", "df.printSchema()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TODO : Use the inferSchdma parameter to infer data types" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = spark.read.csv(\"./cars.csv\", header = True, ## YOUR CODE HERE ##)\n", "\n", "df.printSchema()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show samples from dataframe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.show(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filter all cars made in 2015" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.filter(df['YEAR'] == 2015).show(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TODO: Find all cars made by Tesla" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_tesla = ## YOUR CODE GOES HERE ##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select columns Make, Model and Size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.select(df['Make'], df['Model'], df['Size']).show(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Count manufacturer based on number of cars made" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_manufacturer = df.groupBy(\"Make\").count()\n", "\n", "df_manufacturer.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sort manufacturer based on count of cars made" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_manufacturer.sort(\"count\", ascending=False).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Count and sort the number of cars made by year" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_year = ## YOUR CODE GOES HERE ##\n", "df_year.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert Spark DataFrame to Pandas DataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_pd = df.toPandas()\n", "\n", "df_pd.head(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_pd.describe()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spark.stop()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }