迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

如何在 Pandas 中遍历 DataFrame 的行

作者:迹忆客 最近更新:2024/04/20 浏览次数:

我们可以使用 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 关键字用于定义应用于每行的内联函数。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 Python 中将 Tensor 转换为 NumPy 数组

发布时间:2024/03/12 浏览次数:120 分类:Python

在 Python 中,可以使用 3 种主要方法将 Tensor 转换为 NumPy 数组:Tensor.numpy()函数,Tensor.eval()函数和 TensorFlow.Session()函数。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便