iteritems、iterrows、itertuples——DataFrame的迭代遍历

iteritems、iterrows、itertuples——DataFrame的迭代遍历

Iterate
迭代;重复;重新描述;反复;反覆迭代

1、iteritems()
  • 遍历 DataFrame 成(column name, Series)对.

    • column name——被遍历 DataFrame对象的列标签.
    • Series——以index为index,以该列内容为data.
  • 返回值:(column name, Series)对.

    ☞实例:

    import pandas as pd
    df = pd.DataFrame({'species': ['bear', 'bear', 'marsupial'],
                       'population': [1864, 22000, 80000]},
                       index=['panda', 'polar', 'koala'])
    for label, content in df.iteritems():
       print("label:", label)
       print("content:", content, sep="\n")
    
    输出:
    label: species
    content:
    panda         bear
    polar         bear
    koala    marsupial
    Name: species, dtype: object
    label: population
    content:
    panda     1864
    polar    22000
    koala    80000
    Name: population, dtype: int64
    
2、iterrows()
  • 遍历 DataFrame 成(index, Series)对.

    • index——被遍历 DataFrame对象的行标签.
    • Series——以columns为index,以该行内容为data.
  • 返回值:(index, Series)对.

    ☞实例:

    import pandas as pd
    df = pd.DataFrame({'species': ['bear', 'bear', 'marsupial'],
                       'population': [1864, 22000, 80000]},
                       index=['panda', 'polar', 'koala'])
    for index, content in df.iterrows():
       print("index:", index)
       print("content:", content, sep="\n")
    
    输出:
    index: panda
    content:
    species       bear
    population    1864
    Name: panda, dtype: object
    index: polar
    content:
    species        bear
    population    22000
    Name: polar, dtype: object
    index: koala
    content:
    species       marsupial
    population        80000
    Name: koala, dtype: object
    
  • 注意:

    • 同一个Series中的元素类型应该相等,而iterrows()函数将同行不同列的数据放在一个Series中。我们知道不同列的数据不一定相等,因此在这过程中itrrows()会自动转变元素类型或者报错。

      ☞实例:

      df = pd.DataFrame({'int':[1, 2], 'float':[1.1, 2.2]}, index=['num1', 'num2'])
      
      for index, content in df.iterrows():
         print("index:", index)
         print("content:", content, sep="\n")
      
      输出:
      index: num1
      content:
      int      1.0
      float    1.1
      Name: num1, dtype: float64
      index: num2
      content:
      int      2.0
      float    2.2
      Name: num2, dtype: float64
      
3、itertuples()
  • 遍历 DataFrame 成Pandas(Index=‘ ’, label1=' ', label2=' ',...)具名元祖对.

    • Index——行标签.
    • label1…——列标签.
    • ' '——DataFrame中对应的值 .
  • itertuples(index=true, name='Xxx')

    • index——默认为true, 当指定为False时,输出Pandas(label1=' ', label2=' ',…),即去掉具名元祖第一个index=' '元素.
    • name——指定具名元祖名称,指定后输出为Xxx(Index=‘ ’, label1=' ', label2=' ',…)

    ☞实例:

    for row in df.itertuples():
       print(row)
    
    输出:
    Pandas(Index='panda', species='bear', population=1864)
    Pandas(Index='polar', species='bear', population=22000)
    Pandas(Index='koala', species='marsupial', population=80000)
    
    for row in df.itertuples(index = False, name="Animal"):
       print(row)
    
    实例:
    Animal(species='bear', population=1864)
    Animal(species='bear', population=22000)
    Animal(species='marsupial', population=80000)
    
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值