> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Handle Exceptions With ast.literal_eval in a Pandas
- URL: https://datascientyst.com/how-to-handle-exceptions-with-ast-literal_eval-in-a-pandas/
- Published: 2023-02-06T13:28:59.000Z
- Updated: 2023-02-06T13:28:59.000Z
- Author: John D K
- Tags: apply()

To handle exceptions and use `ast.literal_eval` in Pandas we can define new function:

```python
import ast
import pandas as pd

def parse_eval(value):
	try:
    	return ast.literal_eval(value)
	except (ValueError, SyntaxError):
    	return value

df = pd.DataFrame({'col': ['1', '2', '{"a": 3}', '[[']})
df['col'].apply(parse_eval)

```

result:

```
0       	1
1       	2
2	{'a': 3}
3      	[[
Name: col, dtype: object

```

Otherwise error will be raised:

```python
df['col'].apply(ast.literal_eval)

```

The error is:

`SyntaxError: unexpected EOF while parsing`

## ast.literal\_eval + exception

To use `ast.literal_eval` in Pandas we need the `.apply()` method. This will apply the `ast.literal_eval` function to each value of a column in a Pandas DataFrame.

To handle exceptions in Python we use the `try-except` block - to surround the `ast.literal_eval` call. Finally we return a default value in the except block in case of error.

In the example above, the parse\_eval function takes a string value and:

- returns parsed value from using `ast.literal_eval` \- when possible
- returns the original string - if an exception occurs during the call to `ast.literal_eval`

The `apply` method applies the `parse_eval` function to each element of the 'col' column in the DataFrame df.