# pysparky.functions.general

*module*

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L1-L1)

## pysparky.functions.general.chain

*function*

```python
def chain(func, *args, **kwargs) -> Column
```

Applies a given function to the current Column and returns the result.

This method allows for chaining operations on a Column object by applying
a custom function with additional arguments. It's particularly useful for
creating complex transformations or applying user-defined functions to a Column.

**Parameters**

- `self` (`Column`) — The current Column object.
- `func` (`callable`) — The function to apply to the Column.
- `*args` (default: `()`) — Variable length argument list to pass to the function.
- `**kwargs` (default: `{}`) — Arbitrary keyword arguments to pass to the function.

**Returns**

- `Column` (`Column`) — A new Column object resulting from applying the function.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("hello",)], ["text"])
> >>> def custom_upper(col):
> ...     return F.upper(col)
> >>> result = df.withColumn("upper_text", df.text.chain(custom_upper))
> >>> result.show()
> +-----+----------+
> | text|upper_text|
> +-----+----------+
> |hello|     HELLO|
> +-----+----------+
> 
> >>> def add_prefix(col, prefix):
> ...     return F.concat(F.lit(prefix), col)
> >>> result = df.withColumn("prefixed_text", df.text.chain(add_prefix, prefix="Pre: "))
> >>> result.show()
> +-----+-------------+
> | text|prefixed_text|
> +-----+-------------+
> |hello|   Pre: hello|
> +-----+-------------+
> ```

> **Note**
>
> The function passed to `chain` should expect a Column as its first argument,
> followed by any additional arguments specified in the `chain` call.

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L38-L84)

## pysparky.functions.general.get_value_from_map

*function*

```python
def get_value_from_map(column_or_name: ColumnOrName, dict_: dict) -> Column
```

Retrieves a value from a map (dictionary) using a key derived from a specified column in a DataFrame.

This function creates a map from the provided dictionary and then looks up the value in the map
corresponding to the key that matches the value in the specified column.

**Parameters**

- `column_or_name` (`str`) — The name of the column in the DataFrame whose value will be used as the key to look up in the map.
- `dict_` (`dict`) — A dictionary where keys and values are the elements to be used in the map.

**Returns**

- `Column` (`Column`) — A PySpark Column object representing the value retrieved from the map.

> **Example**
>
> ```python
> >>> map = {1: 'a', 2: 'b'}
> >>> column_name = 'key_column'
> >>> df = spark.createDataFrame([(1,), (2,)], ['key_column'])
> >>> df.withColumn('value', get_value_from_map(map, column_name)).show()
> +----------+-----+
> |key_column|value|
> +----------+-----+
> |         1|    a|
> |         2|    b|
> +----------+-----+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L147-L178)

## pysparky.functions.general.lower_

*function*

```python
def lower_(col: Column) -> Column
```

This serve as an easy Examples on how this package work

**Parameters**

- `col` (`Column`) — The column to be lowercased.

**Returns**

- `Column` (`Column`) — A lowercased column.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("Hello",)], ["text"])
> >>> df.select(lower_(F.col("text"))).show()
> +-----------+
> |lower(text)|
> +-----------+
> |      hello|
> +-----------+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L13-L35)

## pysparky.functions.general.replace_strings_to_none

*function*

```python
def replace_strings_to_none(column_or_name: ColumnOrName, list_of_null_string: list[str], customize_output: Any = None) -> pyspark.sql.Column
```

Replaces empty string values in a column with None.

**Parameters**

- `column_or_name` (`ColumnOrName`) — The column to check for empty string values.

**Returns**

- `Column` (`pyspark.sql.Column`) — A Spark DataFrame column with the values replaced.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("",), ("foo",), (None,)], ["col"])
> >>> df.select(replace_strings_to_none(F.col("col"), [""]).alias("cleaned")).show()
> +-------+
> |cleaned|
> +-------+
> |   null|
> |    foo|
> |   null|
> +-------+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L87-L118)

## pysparky.functions.general.single_space_and_trim

*function*

```python
def single_space_and_trim(column_or_name: ColumnOrName) -> Column
```

Replaces multiple white spaces with a single space and trims the column.

**Parameters**

- `column_or_name` (`Column`) — The column to be adjusted.

**Returns**

- `Column` (`Column`) — A trimmed column with single spaces.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("  foo   bar  ",)], ["text"])
> >>> df.select(single_space_and_trim(F.col("text")).alias("cleaned")).show()
> +-------+
> |cleaned|
> +-------+
> |foo bar|
> +-------+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L121-L144)

## pysparky.functions.general.when_mapping

*function*

```python
def when_mapping(column_or_name: ColumnOrName, dict_: dict) -> Column
```

Applies a series of conditional mappings to a PySpark Column based on a dictionary of conditions and values.

**Parameters**

- `column` (`Column`) — The PySpark Column to which the conditional mappings will be applied.
- `dict_` (`Dict`) — A dictionary where keys are the conditions and values are the corresponding results.

**Returns**

- `Column` (`Column`) — A new PySpark Column with the conditional mappings applied.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("A",), ("B",), ("C",)], ["category"])
> >>> mapping = {"A": 1, "B": 2}
> >>> df.select("category", when_mapping(F.col("category"), mapping).alias("mapped")).show()
> +--------+------+
> |category|mapped|
> +--------+------+
> |       A|     1|
> |       B|     2|
> |       C|  null|
> +--------+------+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/general.py#L181-L214)
