numpy和pandas的遍历

2024-01-02 22:38:54

梳理:

?numpy的遍历:

np.ndenumerate(l1)
np.nditer

pandas的遍历:

pd.iterrows() 遍历行
pd.itertuples(index=False)遍历行
data.iteritems()遍历列
data.columns遍历列
pandas的apply、applymap()、map

?map() 是一个Series的函数,接受一个字典作为参数

ApplyMap将函数做用于DataFrame中的所有元素(elements)

apply()将一个函数作用于DataFrame中的每个行或者列

#python的for循环
    l1=np.arange(8).reshape((2,4))
    for i in l1:
        for j in i:
            print(j)

0、1、2、3、4、5、6、7

#函数nditer()主要用于循环遍历整个数组,而无需为每个额外维度使用嵌套for循环。
    for i in np.nditer(l1):
        print(i)

0、1、2、3、4、5、6、7

# 函数ndenumerate(), 该函数的作用是输出相应的索引号的对应的值。
    for i,j in np.ndenumerate(l1):
        #i是下标,j是下标对应的值
        print(i,j)

?(0, 0) 0
(0, 1) 1
(0, 2) 2
(0, 3) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6
(1, 3) 7

pandas的遍历问题:

# Series创建数据
    ll = np.random.randint(low=1,high=10,size=10) #1到10 之间,随机返回10个数字
    si=pd.Series(ll)
    print(si)

    lf=np.random.randint(1,10,(3,3))
    lk=['A','B','C']
    data = pd.DataFrame(lf,columns=lk)
    print(data)

    # iteritems
    # 遍历每一行
    for index, row in data.iterrows():
        print(f"Index: {index}, Row: {row['A']}, {row['B']}, {row['C']}")

    # 或者
    for row in data.itertuples(index=False):
        print(row[0])
        print(row)

?Index: 0, Row: 2, 5, 9
Index: 1, Row: 6, 4, 7
Index: 2, Row: 5, 5, 9

或者:

2
Pandas(A=2, B=5, C=9)
6
Pandas(A=6, B=4, C=7)
5
Pandas(A=5, B=5, C=9)

#遍历每一列
    # 遍历每一列
    for column, value in data.iteritems():
        print(f"Column: {column}")
        print(value)
    # 或者
    for col in data.columns:
        series = data[col]
        print("列名=====: ", col)
        print(series)

Column: A
0 ? ?2
1 ? ?6
2 ? ?5
Name: A, dtype: int32
Column: B
0 ? ?5
1 ? ?4
2 ? ?5
Name: B, dtype: int32
Column: C
0 ? ?9
1 ? ?7
2 ? ?9

或者:
Name: C, dtype: int32
列名=====: ?A
0 ? ?2
1 ? ?6
2 ? ?5
Name: A, dtype: int32
列名=====: ?B
0 ? ?5
1 ? ?4
2 ? ?5
Name: B, dtype: int32
列名=====: ?C
0 ? ?9
1 ? ?7
2 ? ?9
Name: C, dtype: int32

pandas的apply、applymap()、map
# 应用函数到 DataFrame
    # 方法可以应用一个函数到DataFrame中的每一个元素,返回一个新的DataFrame
    df_new1 = data.apply(add_one)
    print(df_new1)
    # 方法可以应用一个函数到DataFrame中的每一个元素,返回一个新的DataFrame
    df_new2=data.applymap(add_one)
    print(df_new2)
    # 方法可以应用一个函数到Series中的每一个元素,返回一个新的Series
    df_new3 = si.map(si)
    print(df_new3)

?? ?A ?B ? C
0 ?3 ?6 ?10
1 ?7 ?5 ? 8
2 ?6 ?6 ?10


? ?A ?B ? C
0 ?3 ?6 ?10
1 ?7 ?5 ? 8
2 ?6 ?6 ?10


0 ? ?1
1 ? ?5
2 ? ?5
3 ? ?9
4 ? ?1
5 ? ?3
6 ? ?9
7 ? ?9
8 ? ?3
9 ? ?4
dtype: int32

完整代码:

def loopNumpy():
    #python的for循环
    l1=np.arange(8).reshape((2,4))
    for i in l1:
        for j in i:
            print(j)

    #函数nditer()主要用于循环遍历整个数组,而无需为每个额外维度使用嵌套for循环。
    for i in np.nditer(l1):
        print(i)

    # 函数ndenumerate(), 该函数的作用是输出相应的索引号的对应的值。
    for i,j in np.ndenumerate(l1):
        #i是下标,j是下标对应的值
        print(i,j)


    # Series创建数据
    ll = np.random.randint(low=1,high=10,size=10) #1到10 之间,随机返回10个数字
    si=pd.Series(ll)
    print(si)

    lf=np.random.randint(1,10,(3,3))
    lk=['A','B','C']
    data = pd.DataFrame(lf,columns=lk)
    print(data)

    # iteritems
    # 遍历每一行
    for index, row in data.iterrows():
        print(f"Index: {index}, Row: {row['A']}, {row['B']}, {row['C']}")

    # 或者
    for row in data.itertuples(index=False):
        print(row[0])
        print(row)

    #遍历每一列
    # 遍历每一列
    for column, value in data.iteritems():
        print(f"Column: {column}")
        print(value)
    # 或者
    for col in data.columns:
        series = data[col]
        print("列名=====: ", col)
        print(series)

    # 注意pandas中axis=0表示行,axis=1表示列

    # pandas的apply、applymap()、map
    # 应用函数到 DataFrame
    # 方法可以应用一个函数到DataFrame中的每一个元素,返回一个新的DataFrame
    df_new1 = data.apply(add_one)
    print(df_new1)
    # 方法可以应用一个函数到DataFrame中的每一个元素,返回一个新的DataFrame
    df_new2=data.applymap(add_one)
    print(df_new2)
    # 方法可以应用一个函数到Series中的每一个元素,返回一个新的Series
    df_new3 = si.map(si)
    print(df_new3)

# 定义一个函数,对每一个元素加 1
def add_one(x):
    return x + 1


if __name__ == "__main__":
    # qb()
    loopNumpy()

?

文章来源:https://blog.csdn.net/chehec2010/article/details/135348016
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。