**Pandas**

*chevron_left*

**Basic and Descriptive Statistics**

# Pandas DataFrame | product method

*schedule*Aug 10, 2023

*toc*Table of Contents

*expand_more*

**mathematics behind data science**with 100+ top-tier guides

Start your free 7-days trial now!

Pandas `DataFrame.product(~)`

method computes the product for each row or column of the DataFrame.

# Parameters

1. `axis`

link | `int`

or `string`

| `optional`

Whether to compute the product row-wise or column-wise:

Axis | Description |
---|---|

| Product is computed for each column. |

| Product is computed for each row. |

By default, `axis=0`

.

2. `skipna`

link | `boolean`

| `optional`

Whether or not to skip `NaN`

. By default, `skipna=True`

.

3. `level`

| `string`

or `int`

| `optional`

The name or the integer index of the level to consider. This is only relevant if your DataFrame is Multi-index.

4. `numeric_only`

link | `None`

or `boolean`

| `optional`

The allowed values are as follows:

Value | Description |
---|---|

| Only numeric rows/columns will be considered (e.g. |

| Attempt computation with all types (e.g. strings and dates), and throw an error whenever computation is invalid. |

| Attempt computation with all types, and ignore all rows/columns that do not allow for computation without raising an error. |

To compute the product, the `*`

operator must be well-defined between the types.

By default, `numeric_only=None`

.

5. `min_count`

| `int`

| `optional`

The minimum number of values that must be present to compute the product. If there are fewer than `min_count`

values (excluding `NaN`

), then `NaN`

will be returned. By default, `min_count=0`

.

# Return Value

If the `level`

parameter is specified, then a `DataFrame`

will be returned. Otherwise, a `Series`

will be returned.

# Examples

Consider the following DataFrame:

```
df
A B0 2 41 3 5
```

## Column-wise product

To compute the product for each column:

```
df.product() # or axis=0
A 6B 20dtype: int64
```

## Row-wise product

To compute the product for each row, set `axis=1`

:

```
df.product(axis=1)
0 81 15dtype: int64
```

## Specifying skipna

Consider the following DataFrame with a missing value:

```
df
A0 3.01 NaN2 5.0
```

By default, `skipna=True`

, which means that missing values are ignored:

```
df.product() # skipna=True
A 15.0dtype: float64
```

To consider missing values:

```
df.product(skipna=False)
A NaNdtype: float64
```

Note that if a row/column contains one or more missing values, then the product for that row/column would be `NaN`

.

## Specifying numeric_only

Consider the following DataFrame:

```
df
A B C0 4 2 "6"1 5 True "7"
```

Here, both columns `B`

and `C`

are non-numeric, but the key difference is that the product is defined for `B`

, but not for `C`

.

Recall that the internal representation of a `True`

boolean is `1`

, so the operation `2*True`

actually evaluates to `2`

:

```
2 * True
2
```

On the other hand, `"6"*"7"`

throws an error:

```
"6" * "7"
TypeError: can't multiply sequence by non-int of type 'str'
```

### None

By default, `numeric_only=None`

, which means that rows/columns with mixed types will also be considered:

```
df.product() # numeric_only=None
A 20B 2dtype: object
```

Here, notice how the product is not computable for column `C`

because `"6"*"7"`

results in an error. By passing in `None`

, rows/columns that result in these invalid products will simply be ignored without raising an error.

### False

By setting `numeric_only=False`

, rows/columns with mixed types will again be considered, but an error will be thrown when the product cannot be computed:

```
df.product(numeric_only=False)
TypeError: can't multiply sequence by non-int of type 'str'
```

Here, we end up with an error because, as explained, the product `"6"*"7"`

for column `C`

is not defined.

### True

By setting `numeric_only=True`

, only numeric rows/columns will be considered:

```
df.product(numeric_only=True)
A 20dtype: int64
```

Notice how columns `B`

and `C`

were ignored since they contain mixed types.