Pandas Series Series.map() 功能

Minahil Noor 2023年1月30日
  1. pandas.Series.map() 语法
  2. 示例代码:Series.map()
  3. 示例代码:Series.map() 传递一个字典作为 arg 参数
  4. 示例代码:Series.map() 传递一个函数作为 arg 参数
  5. 示例代码:Series.map() 应用于 DataFrame
Pandas Series Series.map() 功能

Python Pandas Series.map() 函数替换一个 Series 的值。被替换的值可能来自于 Series、字典或一个函数。这个函数只对一个 Series 有效。如果我们将此函数应用于 DataFrame,那么它将产生一个 AttributeError

pandas.Series.map() 语法

Series.map(arg, na_action=None)

参数

arg 它是函数,字典或 Series。要替换的值来自这个函数、字典或 Series
na_action 该参数接受两个值。Noneignore。它的默认值是 None。如果它的值是 ignore,那么它就不会将派生值映射到 NaN 值。它忽略 NaN 值。

返回值

它返回一个与调用者具有相同索引的 Series

示例代码:Series.map()

我们将生成一个包含 NaN 值的 Series,以检查传递 na_action 参数后的输出。

import pandas as pd
import numpy as np

series = pd.Series(['Rose', 
                    'Lili', 
                    'Tulip', 
                    np.NaN, 
                    'Orchid', 
                    'Hibiscus', 
                    'Jasmine', 
                    'Daffodil',
                    np.NaN , 
                    'SunFlower', 
                    'Daisy'])

print(series)

示例 Series 是:

0          Rose
1          Lili
2         Tulip
3           NaN
4        Orchid
5      Hibiscus
6       Jasmine
7      Daffodil
8           NaN
9     SunFlower
10        Daisy
dtype: object

我们使用 NumPy 来生成 NaN 值。

参数 arg 是一个强制性参数。如果它没有被传递,那么函数会产生一个 TypeError。我们将首先传递一个 Series 作为 arg 参数。

为了映射两个 Series,第一个 Series 的最后一列应该与第二个 Series 的索引相同。

import pandas as pd
import numpy as np

first_series = pd.Series(
    [
        "Rose",
        "Lili",
        "Tulip",
        np.NaN,
        "Orchid",
        "Hibiscus",
        "Jasmine",
        "Daffodil",
        np.NaN,
        "SunFlower",
        "Daisy",
    ]
)

second_series = pd.Series(
    [23, 34, 67, 90, 21, 45, 29, 70, 56],
    index=[
        "Rose",
        "Lili",
        "Tulip",
        "Orchid",
        "Hibiscus",
        "Jasmine",
        "Daffodil",
        "SunFlower",
        "Daisy",
    ],
)

series1 = first_series.map(second_series)
print(series1)

输出:

0     23.0
1     34.0
2     67.0
3      NaN
4     90.0
5     21.0
6     45.0
7     29.0
8      NaN
9     70.0
10    56.0
dtype: float64

请注意,函数在比较了两个 Series 之后,已经替换了这些值。

示例代码:Series.map() 传递一个字典作为 arg 参数

import pandas as pd
import numpy as np

series = pd.Series(
    [
        "Rose",
        "Lili",
        "Tulip",
        np.NaN,
        "Orchid",
        "Hibiscus",
        "Jasmine",
        "Daffodil",
        np.NaN,
        "SunFlower",
        "Daisy",
    ]
)

dictionary = {
    "Rose": "One",
    "Lili": "Two",
    "Orchid": "Three",
    "Jasmine": "Four",
    "Daisy": "Five",
}

series1 = series.map(dictionary)
print(series1)

输出:

0       One
1       Two
2       NaN
3       NaN
4     Three
5       NaN
6      Four
7       NaN
8       NaN
9       NaN
10     Five
dtype: object

Series 中不在字典中的值会被 NaN 值代替。

示例代码:Series.map() 传递一个函数作为 arg 参数

现在我们将传递一个函数作为参数。

import pandas as pd
import numpy as np

series = pd.Series(
    [
        "Rose",
        "Lili",
        "Tulip",
        np.NaN,
        "Orchid",
        "Hibiscus",
        "Jasmine",
        "Daffodil",
        np.NaN,
        "SunFlower",
        "Daisy",
    ]
)

series1 = series.map("The name of the flower is {}.".format)
print(series1)

输出:

0          The name of the flower is Rose.
1          The name of the flower is Lili.
2         The name of the flower is Tulip.
3           The name of the flower is nan.
4        The name of the flower is Orchid.
5      The name of the flower is Hibiscus.
6       The name of the flower is Jasmine.
7      The name of the flower is Daffodil.
8           The name of the flower is nan.
9     The name of the flower is SunFlower.
10        The name of the flower is Daisy.
dtype: object

这里,我们传递了 string.format() 函数作为参数。请注意,该函数也被应用于 NaN 值。如果我们不想将该函数应用于 NaN 值,那么我们将把 ignore 值传递给 na_action 参数。

import pandas as pd
import numpy as np

series = pd.Series(
    [
        "Rose",
        "Lili",
        "Tulip",
        np.NaN,
        "Orchid",
        "Hibiscus",
        "Jasmine",
        "Daffodil",
        np.NaN,
        "SunFlower",
        "Daisy",
    ]
)

series1 = series.map("The name of the flower is {}.".format, na_action="ignore")
print(series1)

输出:

0          The name of the flower is Rose.
1          The name of the flower is Lili.
2         The name of the flower is Tulip.
3                                      NaN
4        The name of the flower is Orchid.
5      The name of the flower is Hibiscus.
6       The name of the flower is Jasmine.
7      The name of the flower is Daffodil.
8                                      NaN
9     The name of the flower is SunFlower.
10        The name of the flower is Daisy.
dtype: object

上面的示例代码已经忽略了 NaN 值。

示例代码:Series.map() 应用于 DataFrame

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)

dataframe1 = dataframe.map("The flower name is {}.".format)
print(dataframe1)

输出:

AttributeError: 'DataFrame' object has no attribute 'map'

函数产生了 AttributeError

相关文章 - Pandas Series