search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | product method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the 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. axislink | int or string | optional

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

Axis

Description

"index" or 0

Product is computed for each column.

"columns" or 1

Product is computed for each row.

By default, axis=0.

2. skipnalink | 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_onlylink | None or boolean | optional

The allowed values are as follows:

Value

Description

True

Only numeric rows/columns will be considered (e.g. float, int, boolean).

False

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

None

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 = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
   A  B
0  2  4
1  3  5

Column-wise product

To compute the product for each column:

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

Row-wise product

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

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

Specifying skipna

Consider the following DataFrame with a missing value:

df = pd.DataFrame({"A":[3,pd.np.nan,5]})
df
A
0 3.0
1 NaN
2 5.0

By default, skipna=True, which means that missing values are ignored:

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

To consider missing values:

df.product(skipna=False)
A NaN
dtype: 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 = pd.DataFrame({"A":[4,5], "B":[2,True], "C":["6","7"]})
df
   A  B      C
0  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 20
B 2
dtype: 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 20
dtype: int64

Notice how columns B and C were ignored since they contain mixed types.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!