numpy和pandas的遍历

梳理:

 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()

 

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值