- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.__add__#
- DataFrame.__add__(other)[source]#
Get Addition of DataFrame and other, column-wise.
Equivalent to
DataFrame.add(other).- Parameters:
- otherscalar, sequence, Series, dict or DataFrame
Object to be added to the DataFrame.
- Returns:
- DataFrame
The result of adding
otherto DataFrame.
See also
DataFrame.addAdd a DataFrame and another object, with option for index- or column-oriented addition.
Examples
>>>df=pd.DataFrame({'height':[1.5,2.6],'weight':[500,800]},...index=['elk','moose'])>>>df height weightelk 1.5 500moose 2.6 800
Adding a scalar affects all rows and columns.
>>>df[['height','weight']]+1.5 height weightelk 3.0 501.5moose 4.1 801.5
Each element of a list is added to a column of the DataFrame, in order.
>>>df[['height','weight']]+[0.5,1.5] height weightelk 2.0 501.5moose 3.1 801.5
Keys of a dictionary are aligned to the DataFrame, based on column names;each value in the dictionary is added to the corresponding column.
>>>df[['height','weight']]+{'height':0.5,'weight':1.5} height weightelk 2.0 501.5moose 3.1 801.5
Whenother is a
Series, the index ofother is aligned with thecolumns of the DataFrame.>>>s1=pd.Series([0.5,1.5],index=['weight','height'])>>>df[['height','weight']]+s1 height weightelk 3.0 500.5moose 4.1 800.5
Even when the index ofother is the same as the index of the DataFrame,the
Serieswill not be reoriented. If index-wise alignment is desired,DataFrame.add()should be used withaxis=’index’.>>>s2=pd.Series([0.5,1.5],index=['elk','moose'])>>>df[['height','weight']]+s2 elk height moose weightelk NaN NaN NaN NaNmoose NaN NaN NaN NaN
>>>df[['height','weight']].add(s2,axis='index') height weightelk 2.0 500.5moose 4.1 801.5
Whenother is a
DataFrame, both columns names and theindex are aligned.>>>other=pd.DataFrame({'height':[0.2,0.4,0.6]},...index=['elk','moose','deer'])>>>df[['height','weight']]+other height weightdeer NaN NaNelk 1.7 NaNmoose 3.0 NaN