Pandas map() 函数详细说明

您所在的位置:网站首页 series函数如何使用 Pandas map() 函数详细说明

Pandas map() 函数详细说明

2023-03-24 14:12| 来源: 网络整理| 查看: 265

当前位置:主页 > 学无止境 > 编程语言 > WEB前端 编程语言 网络 算法 操作系统 数据库 Pandas map() 函数详细说明 作者:迹忆客 最近更新:2023/03/20 浏览次数:

本文解释了我们如何使用 Series.map() 方法将 Pandas Series 的值替换为另一个值。

import pandas as pd my_series = pd.Series( [85,87,90,89], index=["1","2","3","4"]) print(my_series,"\n")

输出:

1 85 2 87 3 90 4 89 dtype: int64

我们将使用上例中显示的 Series my_series 来解释 Pandas 中 map() 方法的工作。

pandas.Series.map() 方法 语法 Series.map(arg, na_action=None)

它根据 arg 参数替换调用者 Series 对象中的值,返回一个 Series 对象。arg 可以是 function、dictionary 或 Series 对象,它决定了 Series 对象的新值。

na_action 参数可以取 None 或'ignore'作为其值。'ignore'值的 na_action 表示忽略 Series 中的 NaN 值,对它们不做任何处理。

示例:对 Pandas Series 应用 map() 方法 import pandas as pd my_series = pd.Series( [85,87,90,89], index=["1","2","3","4"]) altered_series=my_series.map({85:80,87:80,90:90,89:80}) print("Initial Series:") print(my_series,"\n") print("Altered Series:") print(altered_series,"\n")

输出:

Initial Series: 1 85 2 87 3 90 4 89 dtype: int64 Altered Series: 1 80 2 80 3 90 4 80 dtype: int64

它根据作为 map() 方法参数传递的字典中指定的值,替换 my_series 中的元素。

我们还可以通过 map() 方法使用函数来改变 Pandas 系列的值。

import pandas as pd my_series = pd.Series( [85,87,90,89], index=["1","2","3","4"]) altered_series=my_series.map(lambda x: str(x)+".00") print("Initial Series:") print(my_series,"\n") print("Altered Series:") print(altered_series,"\n")

输出:

Initial Series: 1 85 2 87 3 90 4 89 dtype: int64 Altered Series: 1 85.00 2 87.00 3 90.00 4 89.00 dtype: object

它接收 my_series 中的每一个元素,并在 my_series 中的每一个元素的末尾添加 .00。

示例:使用 map() 方法改变 DataFrame 中的特定列 import pandas as pd df_1 = pd.DataFrame({ 'Column 1': [85,87,90,89], 'Column 2':[55,54,56,66] , 'Column 3': [23,95,65,45], },index=["1","2","3","4"]) print("Initial DataFrame:") print(df_1,"\n") df_1["Column 1"]=df_1["Column 1"].map(lambda x: x*10) print("DataFrame after changing Column 1:") print(df_1)

输出:

Initial DataFrame: Column 1 Column 2 Column 3 1 85 55 23 2 87 54 95 3 90 56 65 4 89 66 45 DataFrame after changing Column 1: Column 1 Column 2 Column 3 1 850 55 23 2 870 54 95 3 900 56 65 4 890 66 45

它将使用 map() 方法只对 DataFrame 的 Column 1 应用 lambda 函数。由于单列是一个 Series 对象,我们可以对 DataFrame 的一列使用 map() 方法。然后我们将 map() 方法返回的 Series 对象赋值回 df_1 DataFrame 的 Column 1。通过这种方式,我们只改变 DataFrame 中特定列的值。

上一篇:Pandas Series.tolist() 函数

下一篇:没有了

相关文章 React 中在 map() 中使用条件跳出map

发布时间:2023/03/15 浏览次数:170 分类:WEB前端

React 中在 map() 中使用条件: 在数组上调用 map() 方法。 使用 if 条件,如果条件满足则显式返回。 否则返回不同的值或返回 null 以不呈现任何内容。 export default function App () { const arr =

jQuery 中的 map() 与 each() 函数

发布时间:2023/03/13 浏览次数:78 分类:WEB前端

本教程演示了 jQuery 中 map() 和 each() 函数的用途和区别。

Java 8 Stream 中 map() 和 flatMap() 的区别

发布时间:2023/02/08 浏览次数:160 分类:编程语言

map() 和 flatmap() 是新功能 Java 8 中的两个重要操作。它们都代表功能操作,它们也是 java.util.stream.Stream 类中的方法,但是 map 用于转换,而 flatmap 用于两者 变换和扁平化,这就是它被称

如何在 TypeScript 中使用带有枚举的 map() 方法

发布时间:2022/12/29 浏览次数:106 分类:编程语言

将 map() 方法与枚举一起使用: 使用 Object.keys() 方法获取枚举键的数组。 使用 map() 方法迭代数组。 在每次迭代中,返回最终数组应包含的值。 // ✅ For String Enums enum Sizes { Small = S , Me

如何在 JS 中以相反顺序在数组上使用 map()

发布时间:2022/12/16 浏览次数:130 分类:WEB前端

要以相反的顺序在数组上使用 map() 方法: 使用 slice() 方法获取数组的副本。 使用 reverse() 方法反转复制的数组。 在反转数组上调用 map() 方法。 const arr = [ a , b , c ]; const mapReverse1 = ar

在 React 中使用带有索引的 map() 方法

发布时间:2022/11/07 浏览次数:94 分类:WEB前端

在 React 中使用带有索引的 map() 方法:在数组上调用 map() 方法。传递给 map() 方法的函数会被元素和索引调用。

在 React 中使用 map() 渲染嵌套数组

发布时间:2022/10/28 浏览次数:186 分类:WEB前端

使用 map() 渲染嵌套数组: 使用 map() 方法迭代外部数组。 在每次迭代中,调用嵌套数组的 map()方法。 渲染嵌套数组的元素。

React 在 map() 中使用条件

发布时间:2022/10/07 浏览次数:154 分类:WEB前端

在 React 中使用 `map()` 中的条件: 在数组上调用 map() 方法。 如果满足,则使用显式返回的 if 条件。 否则返回不同的值或返回 null 以不呈现任何内容。

如何在 React 中跳出 map() 循环

发布时间:2022/09/20 浏览次数:319 分类:WEB前端

在 React 中跳出 map() 循环: 在数组上调用 slice() 方法以获取数组的一部分。 调用数组部分的 map() 方法。遍历数组的一部分。

转载请发邮件至 [email protected] 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3