> For the complete documentation index, see [llms.txt](https://docs.invariant.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.invariant.io/data-science/apache-spark/pyspark-setup/dataframe-api.md).

# DataFrame API

This is a brief overview of the PySpark DataFrame API.&#x20;

#### Init Session

PySpark applications start with initializing `SparkSession`. When running in PySpark shell via pyspark executable, the shell automatically creates the session in the variable spark for users.

```
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()
```

#### Create DataFrame

&#x20;Create a DataFrame using `pyspark.sql.SparkSession.createDataFrame` , which takes the `schema` argument to specify the schema of the DataFrame. If not provided, it can infer the schema by sampling the data.

```
from datetime import datetime, date
import pandas as pd
from pyspark.sql import Row

df = spark.createDataFrame([
    Row(a=16, b=2., c='jane', d=date(2010, 1, 1), e=datetime(2021, 1, 1, 12, 0)),
    Row(a=16, b=3., c='john', d=date(2010, 2, 1), e=datetime(2021, 1, 2, 12, 0)),
    Row(a=32, b=5., c='alex', d=date(2010, 3, 1), e=datetime(2022, 1, 3, 12, 0))
])
df
```

The DataFrame results and schema can be displayed using the commands show below

```
# All DataFrames above result same.
df.show()
df.printSchema()
```

```
+---+---+-----+----------+-------------------+
|   a|  b|   c|         d|                  e|
+----+---+----+----------+-------------------+
|  16|2.0|jane|2000-01-01|2000-01-01 12:00:00|
|  32|3.0|john|2000-02-01|2000-01-02 12:00:00|
|  64|4.0|alex|2000-03-01|2000-01-03 12:00:00|
+----+---+----+----------+-------------------+

root
 |-- a: long (nullable = true)
 |-- b: double (nullable = true)
 |-- c: string (nullable = true)
 |-- d: date (nullable = true)
 |-- e: timestamp (nullable = true)

```

### Viewing Data

The rows of a DataFrame can be displayed using `DataFrame.show()`.

```
df.show(1)
```

```
+----+---+-------+----------+-------------------+
|   a|  b|      c|         d|                  e|
+----+---+-------+----------+-------------------+
|  16|2.0|jane|2000-01-01|2000-01-01 12:00:00|
+----+---+-------+----------+-------------------+
only showing top 1 row

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.invariant.io/data-science/apache-spark/pyspark-setup/dataframe-api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
