pandas apply, map 和 applymap 的区别
本教程解释了 pandas 中 apply()
、map()
和 applymap()
方法之间的区别。
与 applymap()
相关联的函数被应用于给定的 dataframe 的所有元素,因此 applymap()
方法只针对 dataframes 定义。同样,与 apply()
方法相关联的函数可以应用于 dataframe 或 series
的所有元素,因此 apply()
方法是为 series 和 dataframe 对象定义的。pandas 中的 map()
方法只能为 series
对象定义。
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print(df, "\n")
输出:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
我们将使用上例中显示的 dataframe df
来解释 pandas 中 apply()
、map()
和 applymap()
方法之间的区别。
pandas.dataframe.applymap()
语法
dataframe.applymap(func, na_action=none)
它将函数 func
应用于 dataframe
的每个元素。
示例:使用 applymap()
方法来改变 dataframe 中的元素
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print("initial dataframe:")
print(df, "\n")
scaled_df = df.applymap(lambda a: a * 10)
print("scaled dataframe:")
print(scaled_df, "\n")
输出:
initial dataframe:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
scaled dataframe:
col 1 col 2 col 3
a 300 230 850
b 400 350 870
c 500 650 900
d 600 450 890
它对 df
dataframe 中的每个元素相乘,并将结果存储在 scaled_df
dataframe 中。我们将一个 lambda
函数作为参数传递给 applymap()
函数,该函数通过将输入值与 10
相乘返回一个值。所以 dataframe df
中的每一个元素都会被缩放为 10。
我们也可以使用 for
循环来迭代 df
dataframe 中的每个元素,但它使我们的代码可读性降低,凌乱,效率降低。applymap()
是另一种替代方法,可以使代码更具可读性和效率。
除了数学运算,我们还可以对 dataframe 的元素进行其他操作。
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print("initial dataframe:")
print(df, "\n")
altered_df = df.applymap(lambda a: str(a) ".00")
print("altered dataframe:")
print(altered_df, "\n")
输出:
initial dataframe:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
altered dataframe:
col 1 col 2 col 3
a 30.00 23.00 85.00
b 40.00 35.00 87.00
c 50.00 65.00 90.00
d 60.00 45.00 89.00
它在 dataframe df
中的每个元素的末尾添加 .00
。
pandas 中的 map()
方法
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print("initial dataframe:")
print(df, "\n")
df["col 1"] = df["col 1"].map(lambda x: x / 100)
print("dataframe after altering col 1:")
print(df)
输出:
initial dataframe:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
dataframe after altering col 1:
col 1 col 2 col 3
a 0.3 23 85
b 0.4 35 87
c 0.5 65 90
d 0.6 45 89
我们只能对 dataframe 的特定列使用 map()
方法。
pandas 中的 apply()
方法
apply()
方法改变 pandas 中的整个 dataframe
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print("initial dataframe:")
print(df, "\n")
altered_df = df.apply(lambda x: x / 100)
print("altered dataframe:")
print(altered_df, "\n")
输出:
initial dataframe:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
altered dataframe:
col 1 col 2 col 3
a 0.3 0.23 0.85
b 0.4 0.35 0.87
c 0.5 0.65 0.90
d 0.6 0.45 0.89
apply()
方法在 pandas 中只修改某一列
import pandas as pd
df = pd.dataframe(
{
"col 1": [30, 40, 50, 60],
"col 2": [23, 35, 65, 45],
"col 3": [85, 87, 90, 89],
},
index=["a", "b", "c", "d"],
)
print("initial dataframe:")
print(df, "\n")
df["col 1"] = df["col 1"].apply(lambda x: x / 100)
print("dataframe after altering col 1:")
print(df)
输出:
initial dataframe:
col 1 col 2 col 3
a 30 23 85
b 40 35 87
c 50 65 90
d 60 45 89
dataframe after altering col 1:
col 1 col 2 col 3
a 0.3 23 85
b 0.4 35 87
c 0.5 65 90
d 0.6 45 89
因此,从上面的例子中,我们可以看到,apply()
方法可以用来将一个特定的函数应用于整个 dataframe 的所有元素或某一列的所有元素。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串