# Pandas Series - var() function

The Pandas Series var() function returns the unbiased variance over the specified axis. The syntax for using this function is mentioned below:

### Syntax

```Series.var(axis=None, skipna=None, level=None,
ddof=1, numeric_only=None)
```

### Parameters

 `axis` `Optional. `Specify {0 or 'index'}. Specify axis for the function to be applied on. `skipna` `Optional. `Specify True to exclude NA/null values when computing the result. Default is True. `level` `Optional. `Specify level (int or str). If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar. A str specifies the level name. `ddof` `Optional. `Specify Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. `numeric_only` `Optional. `Specify True to include only float, int or boolean data. Default: False

### Return Value

Returns scalar or Series if a level is specified.

### Example: using var() on a Series

In the example below, the var() function is used to get the variance of a given series.

```import pandas as pd
import numpy as np

idx = pd.MultiIndex.from_arrays([
['even', 'even', 'even',
'odd', 'odd', 'odd']],
names=['DataType'])

x = pd.Series([10, 20, 30, 5, 7, 9],
name='Numbers', index=idx)

print("The Series contains:")
print(x)

#var of all values in the series
print("\nx.var() returns:")
print(x.var())

#var of all values within given level
print("\nx.var(level='DataType') returns:")
print(x.var(level='DataType'))
print("\nx.var(level=0) returns:")
print(x.var(level=0))
```

The output of the above code will be:

```The Series contains:
DataType
even    10
even    20
even    30
odd      5
odd      7
odd      9
Name: Numbers, dtype: int64

x.var() returns:
92.3

x.var(level='DataType') returns:
DataType
even    100
odd       4
Name: Numbers, dtype: int64

x.var(level=0) returns:
DataType
even    100
odd       4
Name: Numbers, dtype: int64
```

### Example: using var() on selected series in a DataFrame

Similarly, the var() function can be applied on selected series/column of a given DataFrame. Consider the following example.

```import pandas as pd
import numpy as np

df = pd.DataFrame({
"Bonus": [5, 3, 2, 4],
"Last Salary": [58, 60, 63, 57],
"Salary": [60, 62, 65, 59]},
index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#var of all values of 'Salary' series
print("\ndf['Salary'].var() returns:")
print(df["Salary"].var())
```

The output of the above code will be:

```The DataFrame is:
Bonus  Last Salary  Salary
John       5           58      60
Marry      3           60      62
Sam        2           63      65
Jo         4           57      59

df['Salary'].var() returns:
7.0
```

❮ Pandas Series - Functions

5