Python | Панды dataframe.mul ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas dataframe.mul() function return multiplication of dataframe and other element- wise. This function essentially does the same thing as the dataframe * other, but it provides an additional support to handle missing values in one of the inputs.
Syntax: DataFrame.mul(other, axis=’columns’, level=None, fill_value=None)
Parameters :
other : Series, DataFrame, or constant
axis : For Series input, axis to match Series index on
level : Broadcast across a level, matching Index values on the passed MultiIndex level
fill_value : Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missingReturns : result : DataFrame
Example #1: Use mul() function to find the multiplication of a dataframe with a series.
Note : For multiplication with series, dataframe axis used for multiplication must match series index on.
# importing pandas as pdimport pandas as pd # Creating the first dataframe df1=pd.DataFrame({"A":[14,4,5,4,1], "B":[5,2,54,3,2], "C":[20,20,7,3,8], "D":[14,3,6,2,6]}) # Print the dataframedf1 |

Let’s create the series
# importing pandas as pdimport pandas as pd # create seriessr = pd.Series([3, 2, 4, 5, 6]) # Print seriessr |

Lets use the dataframe.mul() function to perform multiplication
# find multiplication over the index axisdf1.mul(sr, axis = 0) |
Output :
Example #2: Use mul() function to find the multiplication of two datframes. One dataframe contains NA values.
# importing pandas as pdimport pandas as pd # Creating the first dataframe df1=pd.DataFrame({"A":[14,4,5,4,1], "B":[5,2,54,3,2], "C":[20,20,7,3,8], "D":[14,3,6,2,6]}) # Creating the second dataframe with <code>Na</code> valuedf2=pd.DataFrame({"A":[12,4,5,None,1], "B":[7,2,54,3,None], "C":[20,16,11,3,8], "D":[14,3,None,2,6]}) # Print the second dataframedf2 |

Lets use the dataframe.mul() function to find the multiplication of two dataframes, also handle the missing values.
# fill the missing values with 100df1.mul(df2, fill_value = 100) |
Выход :

Обратите внимание, все ячейки с пропущенными значениями были заполнены 100 перед умножением.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.