iterrows和enumerate和for循环

import pandas as pd
import numpy as np

#构造B列为多值,那么B列是字符串,也就是['','',''],这样可以split。不能写成[[],[],[]],这样是list,list不能split。
temp=pd.DataFrame({'A':[1,2,3],'B':['4,2,1','5,3,2','6,4,3']},index=['a','b','c'])
print(temp)
#    A      B
# a  1  4,2,1
# b  2  5,3,2
# c  3  6,4,3
'''iterrows:可输入两个值index,row'''
for index,row in temp[['A','B']].iterrows():
    print(index)
    # a
    # b
    # c
    print(type(index))
    # <class 'str'>
    # <class 'str'>
    # <class 'str'>
    print(row)
    # A         1
    # B   4, 2, 1
    # Name: a, dtype: object
    # A         2
    # B   5, 3, 2
    # Name: b, dtype: object
    # A         3
    # B   6, 4, 3
    # Name: c, dtype: object
    print(type(row))
    # <class 'pandas.core.series.Series'>
    # <class 'pandas.core.series.Series'>
    # <class 'pandas.core.series.Series'>
    print(row['A'])
    # 1
    # 2
    # 3
    print(type(row['A']))
    # <class 'int'>
    # <class 'int'>
    # <class 'int'>
    print(row['B'])
    # 4, 2, 1
    # 5, 3, 2
    # 6, 4, 3
    print(type(row['B']))
    # <class 'str'>
    # <class 'str'>
    # <class 'str'>
    
'''enumerate:可输入两个值index,row'''
for index, row in enumerate(temp[['A','B']]):
    print(index)
    # 0
    # 1
    print(type(index))
    # <class 'int'>
    # <class 'int'>
    print(row)
    # A
    # B
    print(type(row))
    # <class 'str'>
    # <class 'str'>
    print(row['A'])
    # TypeError: string indices must be integers#row就是A B啊
    print(type(row['A']))
    # TypeError: string indices must be integers
    print(row['B'])
    # TypeError: string indices must be integers
    print(type(row['B']))
    # TypeError: string indices must be integers
    
'''除了iterrows和enumerate只能输入一个值'''
for index, row in temp[['A','B']]:
# ValueError: not enough values to unpack(expected 2, got 1)默认只返回一个,只有iterrows和enumerate返回两个
    print(index)
    print(type(index))
    print(row)
    print(type(row))
    print(row['A'])
    print(type(row['A']))
    print(row['B'])
    print(type(row['B']))
    
'''以下几种写法等价'''
for row in temp[['A', 'B']]:
    print(row)
    # A
    # B
    print(type(row))
    # <class 'str'>
    # <class 'str'>
    print(row['A'])
    # TypeError: string indices must be integers因为row就是A B啊
    print(type(row['A']))
    # TypeError: string indices must be integers
    print(row['B'])
    # TypeError: string indices must be integers
    print(type(row['B']))
    # TypeError: string indices must be integers


for row in temp:
    print(row)
    # A
    # B
    print(type(row))
    # <class 'str'>
    # <class 'str'>
    print(row['A'])
    # TypeError: string indices must be integers#row就是A B啊
    print(type(row['A']))
    # TypeError: string indices must be integers
    print(row['B'])
    # TypeError: string indices must be integers
    print(type(row['B']))
    # TypeError: string indices must be integers

for row in temp.keys():
    print(row)
    # A
    # B
    print(type(row))
    # <class 'str'>
    # <class 'str'>
    print(row['A'])
    # TypeError: string indices must be integers#row就是A B啊
    print(type(row['A']))
    # TypeError: string indices must be integers
    print(row['B'])
    # TypeError: string indices must be integers
    print(type(row['B']))
    # TypeError: string indices must be integers

for row in temp.columns:
    print(row)
    # A
    # B
    print(type(row))
    # <class 'str'>
    # <class 'str'>
    print(row['A'])
    # TypeError: string indices must be integers#row就是A B啊
    print(type(row['A']))
    # TypeError: string indices must be integers
    print(row['B'])
    # TypeError: string indices must be integers
    print(type(row['B']))
    # TypeError: string indices must be integers
    
'''最好不用for,用apply比较好'''
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用enumerate()函数在for循环中同时获取元素和对应的索引值。enumerate()函数将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,通过遍历这个索引序列,可以获取到元素和对应的索引值。这个函数的语法如下:enumerate(sequence, start=0)。其中,sequence是一个序列、迭代器或其他支持迭代对象,start是可选参数,表示索引的起始位置,默认为0。函数返回的是一个enumerate对象,是一个可迭代对象,可以通过遍历取出元素和索引值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 使用enumerate()函数详解](https://blog.csdn.net/jh035/article/details/128077895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python遍历序列enumerate函数浅析](https://download.csdn.net/download/weixin_38742124/14861800)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值