Python3, data processing and calculation, the prod() function of the efficient calculation function that has to be mastered,

@TOC

1 Introduction

Little Diaosi : Brother Yu, do you know the prod() function?
Xiaoyu : Are you asking me to slap me in the face?
Little Diaosi : Then how should I ask?
Xiaoyu : You should ask: Brother Yu, can you tell me about the prod() function.
Little Diaosi : Brother Yu, I can't say that.
Xiaoyu : ... why, why, why?
Little Diaosi : Because, I have been exercising recently.
insert image description here

Xiaoyu : Damn...
Xiao Diaosi : What's wrong, brother Yu, I'm in a hurry.
Xiaoyu : No, I will directly talk about the prod() function.
Little Diaosi : No, brother Yu, today's progress is a bit ahead of schedule.
Xiaoyu : I'm going to the gym.
Little Diaosi : I ca...

In the previous article, we learned about the differences and applicable scenarios between numpy and pandas.
Today, we continue our in-depth exploration of the **prod()** function of numpy and pandas.

2. prod() function

2.1 Definition

The prod() function is a mathematical function in Python that calculates the product of all elements in a given iterable.

usage:

math.prod(iterable, *, start=1)

Parse:

  • iterable is an iterable object, which can be a list, tuple, collection, etc.;
  • start is an optional parameter, indicating the initial value of the product, the default is 1

2.2 Code example

2.2.1 numpy's prod() function

code example

# -*- coding:utf-8 -*-
# @Time   : 2023-07-04
# @Author : Carl_DJ
'''
实现功能:
	使用 NumPy prod() 计算数组元素乘积
'''
import numpy as np

#创建array
arr = np.array([1, 2, 3, 4, 5])
result = np.prod(arr)

print(result)

Parse:

  • First, we imported the NumPy library and renamed it to np.
  • Creates a NumPy array called arr containing integers from 1 to 5.
  • Use the np.prod() function to calculate the product of all elements in the arr array, and assign the result to the result variable.
  • Finally, use the print() function to print out the result.

2.2.2 The prod() function of pandas

# -*- coding:utf-8 -*-
# @Time   : 2023-07-04
# @Author : Carl_DJ
'''
实现功能:
	使用 Pandas prod() 计算乘积
'''

import pandas as pd

#创建DataFrame
df = pd.DataFrame({
    
    'A': [1, 2, 3], 'B': [4, 5, 6]})
result = df.prod()

print(result)

Parse:

  • First, we imported the Pandas library and renamed it pd.
  • Created a DataFrame called df, which contains two columns (A and B) with three integers in each column.
  • The product of each column in df is calculated using the df.prod() function, and the result is assigned to the result variable.
  • Finally, use the print() function to print out the result.

3. Summary

Seeing this, today's content is almost over.
Today we continued our in-depth exploration of the prod() function of numpy and pandas.

Looking at this sample code, do you think it is very simple?
If you feel that way, you are right.
Because, learning new functions is that simple.

I am a small fish :

  • CSDN blog expert ;
  • Aliyun expert blogger ;
  • 51CTO blog expert ;
  • 51 certified lecturer, etc .;
  • Certified gold interviewer ;
  • Job interview and training planner ;
  • Certified expert bloggers in several domestic mainstream technical communities ;
  • Winners of the first and second prizes in the evaluation of various mainstream products (Alibaba Cloud, etc.) ;

Follow me and take you to learn more, more professional and prefaced Python technology.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/131532615