Functions

Math

SQRT

# How to Use Excel's SQRT Function in Pandas

Excel's SQRT function returns the positive square root of a given number. It's a fundamental mathematical operation commonly used in geometry, physics, finance, and many other fields.

This page explains how to use Excel's SQRT function in Python using pandas.

## Implementing the Square Root function in Pandas#

To replicate the SQRT function in Excel using Python and pandas, you can utilize the numpy library, which provides a square root function that works seamlessly with pandas.

### Single value#

Finding the square root of a single value in Excel is straightforward using the SQRT function. For instance, =SQRT(16) would return 4.

In pandas, the equivalent can be achieved using numpy's sqrt function:

```
import numpy as np
sqrt_value = np.sqrt(16)
```

### Entire column#

If you want to compute the square root of each value in a column in Excel, you would apply the SQRT function to each cell of the column.

To compute the square root of an entire column in pandas, apply numpy's sqrt function to the desired DataFrame column

```
# Calculate the square root of each number
df['sqrt'] = np.sqrt(df['number'])
```

## Common mistakes when using SQRT in Python#

While applying the SQRT function in pandas is straightforward, there are some pitfalls to watch out for. Here are some common mistakes and how to address them.

### Negative Values#

Just like in Excel, attempting to find the square root of negative numbers using numpy's sqrt function will not return a number. In Excel it returns the #NUM! error, while in pandas it returns NaN (not a number).

```
# Filter out negative values before applying square root
df = df[df['original_column'] >= 0]
df['sqrt_column'] = np.sqrt(df['original_column'])
```

### Data Type Mismatch#

Attempting to apply the square root function on non-numeric columns will raise a TypeError.

Ensure that the column you're applying the square root function to is of a numeric data type.

```
# Convert column to numeric type if it isn't already
df['original_column'] = pd.to_numeric(df['original_column'], errors='coerce')
df['sqrt_column'] = np.sqrt(df['original_column'])
```

## Understanding the Square Root Formula in Excel#

The SQRT function in Excel takes a single number and returns its positive square root.

=SQRT(number)

### SQRT Excel Syntax

Parameter | Description | Data Type |
---|---|---|

number | The number you want to find the square root of. | number |

### Examples

Formula | Description | Result |
---|---|---|

=SQRT(4) | Calculate the square root of 4. | 2 |

=SQRT(81) | Calculate the square root of 81. | 9 |

=SQRT(-4) | Calculate the square root of -4. | #NUM! |

**Don't re-invent the wheel. Use Excel formulas in Python.**

## Don't want to re-implement Excel's functionality in Python?

## Edit a spreadsheet.

Generate Python.

Mito is the easiest way to write Excel formulas in Python. Every edit you make in the Mito spreadsheet is automatically converted to Python code.

View all 100+ transformations →