# PySpark Action Methods

* Action methods are operations that return a value or produce a side effect. They are used to retrieve or collect data from a DataFrame.
* One of the most commonly used action methods is `count()` which returns the number of rows in a DataFrame.

```python
df.count()
```

* Another commonly used action method is `show()` which displays the first n rows of a DataFrame. By default, it shows 20 rows, but you can specify a different number of rows.

```python
df.show(n=10)
```

* `collect()` method is used to retrieve all the rows in a DataFrame as an array of Row objects. It should be used with caution as it can cause the driver to run out of memory if the DataFrame is too large.

```python
df.collect()
```

* `first()` method is used to retrieve the first row in a DataFrame.

```python
df.first()
```

* `take()` method is used to retrieve the first n rows of a DataFrame.

```python
df.take(n=5)
```

* `foreach()` method is used to apply a function to each element of a DataFrame.

<pre class="language-python"><code class="lang-python">def my_function(row):
    print(row)
<strong>
</strong><strong>df.foreach(my_function)
</strong></code></pre>

* `foreachPartition()` method is used to apply a function to each partition of a DataFrame.

```python
def my_function(iterator):
    for row in iterator:
        print(row)

df.foreachPartition(my_function)
```

* `toPandas()` method is used to convert a DataFrame to a pandas DataFrame. It should be used with caution as it can cause the driver to run out of memory if the DataFrame is too large.

```python
df.toPandas()
```

These are some of the commonly used action methods in PySpark. There are many more methods and options available depending on the specific use case. It's always good to check the [documentation](https://docs.databricks.com/) for the latest updates and options.


---

# Agent Instructions: 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:

```
GET https://docs.consoleflare.com/pyspark-and-databricks/pyspark-action-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
