Python | Панды dataframe.add ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Dataframe.add() method is used for addition of dataframe and other, element-wise (binary operator add). Equivalent to dataframe + other, but with support to substitute a fill_value for missing data in one of the inputs.
Syntax: DataFrame.add(other, axis=’columns’, level=None, fill_value=None)
Parameters:
other :Series, DataFrame, or constant
axis :{0, 1, ‘index’, ‘columns’} For Series input, axis to match Series index on
fill_value : [None or float value, default None] Fill missing (NaN) values with this value. If both DataFrame locations are missing, the result will be missing.
level : [int or name] Broadcast across a level, matching Index values on the passed MultiIndex levelReturns: result DataFrame
# Importing Pandas as pdimport pandas as pd # Importing numpy as npimport numpy as np # Creating a dataframe# Setting the seed value to re-generate the result.np.random.seed(25) df = pd.DataFrame(np.random.rand(10, 3), columns =["A", "B", "C"]) # np.random.rand(10, 3) has generated a# random 2-Dimensional array of shape 10 * 3# which is then converted to a dataframe df |

Note: add() function is similar to ‘+’ operation but, add() provides additional support for missing values in one of the inputs.
# We want NaN values in dataframe. # so let"s fill the last row with NaN valuedf.iloc[-1] = np.nan df |

Adding a constant value to the dataframe using add() function:
# add 1 to all the elements# of the data framedf.add(1) |

Notice the output above, no addition took place for the nan cells in the df dataframe.add() function has an attribute fill_value. This will fill the missing value(Nan) with the assigned value. If both dataframe values are missing then, the result will be missing.
Let’s see how to do it.
# We have given a default value# of "10" for all the nan cellsdf.add(1, fill_value = 10) |

Все наноячейки были сначала заполнены 10, а затем к нему добавляется 1.
Добавление серии в Dataframe:
For Series input, the dimension of the indexes must match for both data frame and series.
# Create a Series of 10 valuestk = pd.Series(np.ones(10)) # tk is a Series of 10 elements# all filled with 1 |

# Add tk(series) to the df(dataframe)# along the index axisdf.add(tk, axis ="index") |

Adding one data frame with other data frame
# Create a second dataframe# First set the seed to regenerate the resultnp.random.seed(10) # Create a 5 * 5 dataframedf2 = pd.DataFrame(np.random.rand(5, 5), columns =["A", "B", "C", "D", "E"]) df2 |

Let’s perform element-wise addition of these two data frames
df.add(df2) |

Обратите внимание, что результирующий фрейм данных имеет размер 10 * 5 и имеет значение nan во всех тех ячейках, для которых любой из фреймов данных имеет значение nan.
Let’s fix it –
# Set a default value of 10 for nan cells# nan value won"t be filled for those cells# in which both data frames has nan valuedf.add(df2, fill_value = 10) |

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