📜  Python|熊猫 dataframe.radd()

📅  最后修改于: 2022-05-13 01:55:26.919000             🧑  作者: Mango

Python|熊猫 dataframe.radd()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.radd()函数执行数据帧和其他对象元素的添加。其他对象可以是常量、系列或数据框。该函数本质上执行其他 + 数据帧,但额外支持 fill_value,填充一个输入中的所有缺失值。
对于系列输入,索引必须匹配。
注意:这与 dataframe.add()函数不同。在这个函数中,我们将数据框添加到另一个对象。

示例 #1:使用 radd()函数执行添加数据帧和系列

Python3
# importing pandas as pd
import pandas as pd
 
# Creating the dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                   "B":[3, 2, 4, 3, 4],
                   "C":[2, 2, 7, 3, 4],
                   "D":[4, 3, 6, 12, 7]})
 
# Print the dataframe
df


Python3
# importing pandas as pd
import pandas as pd
 
# Create a Series
sr = pd.Series([5, 10, 15, 20], index =["A", "B", "C", "D"])
 
# Print the series
sr


Python3
# add dataframe to the series over the column axis
df.radd(sr, axis = 1)


Python3
# importing pandas as pd
import pandas as pd
 
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                    "B":[3, 2, 4, 3, 4],
                    "C":[2, 2, 7, 3, 4],
                    "D":[4, 3, 6, 12, 7]})
 
# Creating the second dataframe
df2 = pd.DataFrame({"A":[14, 5, None, 4, 12],
                    "B":[7, 6, 4, 5, None],
                    "C":[2, 11, 4, 3, 6],
                    "D":[4, None, 6, 2, 4]})
 
# add two dataframes
df1.radd(df2, fill_value = 100)


让我们创建一个与数据框列轴匹配索引的系列

Python3

# importing pandas as pd
import pandas as pd
 
# Create a Series
sr = pd.Series([5, 10, 15, 20], index =["A", "B", "C", "D"])
 
# Print the series
sr

现在,使用 dataframe.radd()函数执行加法。

Python3

# add dataframe to the series over the column axis
df.radd(sr, axis = 1)

输出 :


示例 #2:使用 radd()函数逐元素添加两个数据帧

Python3

# importing pandas as pd
import pandas as pd
 
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],
                    "B":[3, 2, 4, 3, 4],
                    "C":[2, 2, 7, 3, 4],
                    "D":[4, 3, 6, 12, 7]})
 
# Creating the second dataframe
df2 = pd.DataFrame({"A":[14, 5, None, 4, 12],
                    "B":[7, 6, 4, 5, None],
                    "C":[2, 11, 4, 3, 6],
                    "D":[4, None, 6, 2, 4]})
 
# add two dataframes
df1.radd(df2, fill_value = 100)

输出 :

熊猫-dataframe-rad