如何在 Pandas 中遍历 DataFrame 的行

Suraj Joshi 2023年1月30日
  1. 使用 index 属性来遍历 Pandas DataFrame 中的行
  2. loc[] 方法来遍历 Python 中的 DataFrame 行
  3. 在 Python 中用 iloc[] 方法遍历 DataFrame 行
  4. pandas.DataFrame.iterrows() 遍历 Pandas 行
  5. pandas.DataFrame.itertuples 遍历 Pandas 行
  6. ##pandas.DataFrame.apply 遍历 Pandas 行
如何在 Pandas 中遍历 DataFrame 的行

我们可以使用 DataFrame 的 index 属性遍历 Pandas DataFrame 的行。我们还可以使用 DataFrame 对象的 loc()iloc()iterrows()itertuples()iteritems()apply() 方法遍历 Pandas DataFrame 的行。

在以下各节中,我们将使用以下 DataFrame 作为示例。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})

print(df)

输出:

       Date  Income_1  Income_2
0  April-10        10        20
1  April-11        20        30
2  April-12        10        10
3  April-13        15         5
4  April-14        10        40
5  April-16        12        13

使用 index 属性来遍历 Pandas DataFrame 中的行

Pandas DataFrame 的 index 属性提供了从 DataFrame 的顶行到底行的范围对象。我们可以使用范围来迭代 Pandas 中的行。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})

for i in df.index:
    print(
        "Total income in "
        + df["Date"][i]
        + " is:"
        + str(df["Income_1"][i] + df["Income_2"][i])
    )

输出:

Total income in April-10 is:30
Total income in April-11 is:50
Total income in April-12 is:20
Total income in April-13 is:20
Total income in April-14 is:50
Total income in April-16 is:25

它将每行的 Income_1Income_2 相加并打印总收入。

loc[] 方法来遍历 Python 中的 DataFrame 行

loc[] 方法用于一次访问一行。当我们在遍历 DataFrame 的循环中使用 loc[] 方法时,我们可以遍历 DataFrame 的行。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})

for i in range(len(df)):
    print(
        "Total income in "
        + df.loc[i, "Date"]
        + " is:"
        + str(df.loc[i, "Income_1"] + df.loc[i, "Income_2"])
    )

输出:

Total income in April-10 is:30
Total income in April-11 is:50
Total income in April-12 is:20
Total income in April-13 is:20
Total income in April-14 is:50
Total income in April-16 is:25

在这里,range(len(df)) 生成一个范围对象以遍历 DataFrame 中的整个行。

在 Python 中用 iloc[] 方法遍历 DataFrame 行

Pandas DataFrame 的 iloc 属性也非常类似于 loc 属性。loc 和 iloc 之间的唯一区别是,在 loc 中,我们必须指定要访问的行或列的名称,而在 iloc 中,我们要指定要访问的行或列的索引。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})

for i in range(len(df)):
    print(
        "Total income in " + df.iloc[i, 0] + " is:" + str(df.iloc[i, 1] + df.iloc[i, 2])
    )

输出:

Total income in April-10 is:30
Total income in April-11 is:50
Total income in April-12 is:20
Total income in April-13 is:20
Total income in April-14 is:50
Total income in April-16 is:25

这里的索引 0 代表 DataFrame 的第一列,即 Date,索引 1 代表 Income_1 列,索引 2 代表 Income_2 列。

pandas.DataFrame.iterrows() 遍历 Pandas 行

pandas.DataFrame.iterrows() 返回的索引该行以及该行的整个数据为系列。因此,我们可以使用此函数在 Pandas DataFrame 中的行上进行迭代。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})


for index, row in df.iterrows():
    print(
        "Total income in "
        + row["Date"]
        + " is:"
        + str(row["Income_1"] + row["Income_2"])
    )

输出:

Total income in April-10 is:30
Total income in April-11 is:50
Total income in April-12 is:20
Total income in April-13 is:20
Total income in April-14 is:50
Total income in April-16 is:25

pandas.DataFrame.itertuples 遍历 Pandas 行

pandas.DataFrame.itertuples 返回一个对象,以使用第一个字段作为索引,其余字段作为列值。因此,我们还可以使用此函数在 Pandas DataFrame 中的行上进行迭代。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})


for row in df.itertuples():
    print("Total income in " + row.Date + " is:" + str(row.Income_1 + row.Income_2))

输出:

Total income in April-10 is:30
Total income in April-11 is:50
Total income in April-12 is:20
Total income in April-13 is:20
Total income in April-14 is:50
Total income in April-16 is:25

##pandas.DataFrame.apply 遍历 Pandas 行

pandas.DataFrame.apply 返回一个 DataFrame
沿 DataFrame 的给定轴应用给定函数的结果。

语法:

DataFrame.apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwds)

其中,func 代表要应用的函数,而 axis 代表应用函数的轴。我们可以使用 axis = 1axis ='columns' 将函数应用于每一行。

import pandas as pd

dates = ["April-10", "April-11", "April-12", "April-13", "April-14", "April-16"]
income1 = [10, 20, 10, 15, 10, 12]
income2 = [20, 30, 10, 5, 40, 13]

df = pd.DataFrame({"Date": dates, "Income_1": income1, "Income_2": income2})


print(
    df.apply(
        lambda row: "Total income in "
        + row["Date"]
        + " is:"
        + str(row["Income_1"] + row["Income_2"]),
        axis=1,
    )
)

输出:

0    Total income in April-10 is:30
1    Total income in April-11 is:50
2    Total income in April-12 is:20
3    Total income in April-13 is:20
4    Total income in April-14 is:50
5    Total income in April-16 is:25
dtype: object

此处,lambda 关键字用于定义应用于每行的内联函数。

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame

相关文章 - Pandas DataFrame Row