精华内容
下载资源
问答
  • pandas.DataFrame迭代

    2020-03-23 11:48:02
    文章目录1. df.iteritems()2. df.iterrows()3.... 直接迭代 1. df.iteritems() Administrator@cibpc-019 MINGW64 / $ ipython Python 3.6.7 (default, Jul 2 2019, 02:21:41) [MSC v.1900 64 bit (AMD64)] T...

    1. df.iteritems()

    Administrator@cibpc-019 MINGW64 /
    $ ipython
    Python 3.6.7 (default, Jul  2 2019, 02:21:41) [MSC v.1900 64 bit (AMD64)]
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: import pandas as pd
    
    In [2]: df = df = pd.DataFrame({'house' : list('AABCEFG'),
       ...:                    'price' : [100, 90, '', 50, 120, 150, 200],
       ...:                    'toward' : ['1','1','2','3','','3','2']})
       ...:
    
    In [3]: df = pd.DataFrame({'house' : list('AABCEFG'),'price' : [100, 90, '', 50, 120, 150, 200],'toward' : ['1','1','2','3','','
       ...: 3','2']})
    
    In [4]: df
    Out[4]:
      house price toward
    0     A   100      1
    1     A    90      1
    2     B            2
    3     C    50      3
    4     E   120
    5     F   150      3
    6     G   200      2
    
    In [5]: for a in df.iteritems():
        ...:     print(a)
        ...:
    ('house', 0    A
    1    A
    2    B
    3    C
    4    E
    5    F
    6    G
    Name: house, dtype: object)
    ('price', 0    100
    1     90
    2
    3     50
    4    120
    5    150
    6    200
    Name: price, dtype: object)
    ('toward', 0    1
    1    1
    2    2
    3    3
    4
    5    3
    6    2
    Name: toward, dtype: object)
    
    In [6]: for key, val in df.iteritems():
        ...:     print(f'key: {key}, val: {val}')
        ...:
        ...:
    key: house, val: 0    A
    1    A
    2    B
    3    C
    4    E
    5    F
    6    G
    Name: house, dtype: object
    key: price, val: 0    100
    1     90
    2
    3     50
    4    120
    5    150
    6    200
    Name: price, dtype: object
    key: toward, val: 0    1
    1    1
    2    2
    3    3
    4
    5    3
    6    2
    Name: toward, dtype: object
    
    
    
    In [8]: for key, val in df.iteritems():
       ...:     print(f'key: {key}, val: {type(val)}')
       ...:
       ...:
    key: house, val: <class 'pandas.core.series.Series'>
    key: price, val: <class 'pandas.core.series.Series'>
    key: toward, val: <class 'pandas.core.series.Series'>
    

    2. df.iterrows()

    
    In [9]: for c in df.iterrows():
        ...:     print(c)
        ...:
    (0, house       A
    price     100
    toward      1
    Name: 0, dtype: object)
    (1, house      A
    price     90
    toward     1
    Name: 1, dtype: object)
    (2, house     B
    price
    toward    2
    Name: 2, dtype: object)
    (3, house      C
    price     50
    toward     3
    Name: 3, dtype: object)
    (4, house       E
    price     120
    toward
    Name: 4, dtype: object)
    (5, house       F
    price     150
    toward      3
    Name: 5, dtype: object)
    (6, house       G
    price     200
    toward      2
    Name: 6, dtype: object)
    
    In [10]: for key, val in df.iterrows():
        ...:     print(f'key: {key}, val: {val}')
        ...:
    key: 0, val: house       A
    price     100
    toward      1
    Name: 0, dtype: object
    key: 1, val: house      A
    price     90
    toward     1
    Name: 1, dtype: object
    key: 2, val: house     B
    price
    toward    2
    Name: 2, dtype: object
    key: 3, val: house      C
    price     50
    toward     3
    Name: 3, dtype: object
    key: 4, val: house       E
    price     120
    toward
    Name: 4, dtype: object
    key: 5, val: house       F
    price     150
    toward      3
    Name: 5, dtype: object
    key: 6, val: house       G
    price     200
    toward      2
    Name: 6, dtype: object
    
    In [11]: for key, val in df.iterrows():
        ...:     print(f'key: {key}, val: {type(val)}')
        ...:
        ...:
    key: 0, val: <class 'pandas.core.series.Series'>
    key: 1, val: <class 'pandas.core.series.Series'>
    key: 2, val: <class 'pandas.core.series.Series'>
    key: 3, val: <class 'pandas.core.series.Series'>
    key: 4, val: <class 'pandas.core.series.Series'>
    key: 5, val: <class 'pandas.core.series.Series'>
    key: 6, val: <class 'pandas.core.series.Series'>
    

    3. df.itertuples()

    In [12]: for d in df.itertuples():
        ...:     print(d)
        ...:
    Pandas(Index=0, house='A', price=100, toward='1')
    Pandas(Index=1, house='A', price=90, toward='1')
    Pandas(Index=2, house='B', price='', toward='2')
    Pandas(Index=3, house='C', price=50, toward='3')
    Pandas(Index=4, house='E', price=120, toward='')
    Pandas(Index=5, house='F', price=150, toward='3')
    Pandas(Index=6, house='G', price=200, toward='2')
    

    4. 直接迭代

    In [15]:for e in df:
        ...:     print(e)
        ...:
    house
    price
    toward
    

    5. columns, index, values迭代

    In [16]: for f in df.columns:
        ...:     print(f)
        ...:
    house
    price
    toward
    
    In [17]: for g in df.indexs:
        ...:     print(g)
        ...:
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-20-7459c53beff9> in <module>
    ----> 1 for g in df.indexs:
          2     print(g)
          3
    
    D:\Anaconda3\envs\py36\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
       5177             if self._info_axis._can_hold_identifiers_and_holds_name(name):
       5178                 return self[name]
    -> 5179             return object.__getattribute__(self, name)
       5180
       5181     def __setattr__(self, name, value):
    
    AttributeError: 'DataFrame' object has no attribute 'indexs'
    
    In [18]: for g in df.index:
        ...:     print(g)
        ...:
    0
    1
    2
    3
    4
    5
    6
    
    In [19]: for h in df.values:
        ...:     print(h)
        ...:
    ['A' 100 '1']
    ['A' 90 '1']
    ['B' '' '2']
    ['C' 50 '3']
    ['E' 120 '']
    ['F' 150 '3']
    ['G' 200 '2']
    
    
    展开全文
  • row['given_percent']=1 row['remain_amount']=0 row['remain_percent']=0 total_amount=total_amount-row['amount'] #df_debts.iloc[index]=row #此法不可行,会导致重新迭代。 df.loc[df['sno']==row['sno']]=row...

    方法1:df.loc[conditions]=row.values,逐行地进行整行替换

    for row in df.iterrows():
        row['given_amount']=row['amount'];
        row['given_percent']=1
        row['remain_amount']=0
        row['remain_percent']=0
        total_amount=total_amount-row['amount']
        #df_debts.iloc[index]=row #此法不可行,会导致重新迭代。
        df.loc[df['sno']==row['sno']]=row.values
    

     方法2:用loc,批量赋值替换

    indexer=df.loc[condition]
    df.loc[indexer,'col_1']=new_value
    df.loc[indexer,'col_2']=df.loc[indexer,'col_3']*10
    
    展开全文
  • 迭代iter 2018/12/1 ======================================================================= 1.基本iteration()产生:#系列:值;...其他数据结构如DataFrame遵循迭代对象“键”的类似dict ...
    迭代iter   2018/12/1
        
    =======================================================================
    1.基本iteration()产生:#系列:值;DataFrame:列标签;面板:项目标签
    # 迭代Series类似数组,迭代产生值。其他数据结构如DataFrame遵循迭代对象“键”的类似dict
        
    s = pd.Series( [1,2,3],index=['a', 'b', 'c'])
    for col in s:
        print(col,end=",")  #1 ,2 ,3,
    
    df = pd.DataFrame({'col1' : [1,2,3], 'col2' : [4,5.0,6]},index=['a', 'b', 'c'])
    for col in df:
           print(col)           # col1 col2
    =======================================================================
        
    2.iteritems 类似dict遍历键值对:
    # 系列 :(索引,标量值)对;DataFrame :(列,系列)对;面板 :( item,DataFrame)对
        
    for index,value in s.iteritems():
        print('(%s,%s)'%(index,value,),end='') # (a,1)(b,2)(c,3)
        
    for col,s0 in df.iteritems():
        print(col)
        print(s0)
    # col1
    # a    1
    # b    2
    # c    3
    # Name: col1, dtype: int64
    # col2
    # a    4.0
    # b    5.0
    # c    6.0
    # Name: col2, dtype: float64
    =====================================================================
    3.迭代DataFrame行
        
    iterrows()迭代DataFrame行返回迭代器,产生索引值及每行Series;没有保留跨行dtypes
    itertuples()返回迭代器,为DataFrame每一行产生一个namedtuple。
        # 元组的第一个元素是行的相应索引值,而其余值是行值。
        #  itertuples()保留值的数据类型快于iterrows()
        
    # 实例1:
    for row_index, row in df.iterrows():
           print('%s\n%s' % (row_index, row))
           
    #  a
    # col1    1.0
    # col2    4.0
    # Name: a, dtype: float64
    # b
    # col1    2.0
    # col2    5.0
    # Name: b, dtype: float64
    # c
    # col1    3.0
    # col2    6.0
    # Name: c, dtype: float64
        
    # 实例2:
    for row in df.itertuples():
           print(row)
        
    # Pandas(Index='a', col1=1, col2=4.0)
    # Pandas(Index='b', col1=2, col2=5.0)
    # Pandas(Index='c', col1=3, col2=6.0)
    ======================================================================
    4.备注
        # pandas对象迭代通常很慢。在许多情况下,不需要在行上迭代,用以下方法之一避免:
        # 矢量化:内置方法或NumPy函数(布尔)索引,若无函数可用最好用apply()而不是迭代值。
        # 性能很重要用cython或numba编写内部循环。
        
    警告
        迭代器返回副本而不是视图,写入它将不起作用!
    ======================================================================
    展开全文
  • iteritems、iterrows、itertuples——DataFrame迭代遍历 Iterate 迭代;重复;重新描述;反复;反覆迭代 1、iteritems() 遍历 DataFrame 成(column name, Series)对. column name——被遍历 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)
      
    展开全文
  • 最近预处理一些数据时遇到一个问题,我想利用一个循环,每次从一个dataframe中选取特定的数据,再把这些数据重新组合成为一个新的dataframe,但网上很多关于pandas或者numpy的教程中都很少有讨论过如何实现这样的...
  • 利用itertuples对DataFrame进行遍历

    千次阅读 2019-05-09 14:53:51
    最近在做推荐系统实践的时候需要生成物品同现矩阵和...iterrows() : 将DataFrame迭代成(index ,series) iteritems(): 将DataFrame迭代成(列名,series) itertuples(): 将DataFrame迭代成元组 示例如下: ...
  • iterrows(), iteritems(), itertuples()对...iterrows(): 将DataFrame迭代为(insex, Series)对。 itertuples(): 将DataFrame迭代为元祖。 iteritems(): 将DataFrame迭代为(列名, Series)对 参考做梦当财神 的博客
  • 前几天在做决策树预测股票涨跌的小项目,里面要对DataFrame类型的变量进行迭代修改,我用的是iterrows的index和row 但是!测试了好久的代码,df的值始终没变化,一丁点都没有! 直到后来有篇帖子点到 row[" "]修改的...
  • iterrows():将DataFrame迭代为(insex, Series)对。 itertuples():将DataFrame迭代为元祖。 iteritems():将DataFrame迭代为(列名, Series)对 有如下DataFrame数据 import pandas as pd inp = [{'c1':10, 'c2':...
  • from:... How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代 https://stackoverflow.com/questions/16476924/how-to-iterate-over...
  • Pandas 迭代

    2020-04-03 00:08:00
    其他数据结构,如:DataFrame和Panel,遵循类似惯例迭代对象的键。 简而言之,基本迭代(对于i在对象中)产生 - Series - 值 DataFrame - 列标签 Pannel - 项目标签 迭代DataFrame 迭代DataFrame提供列名。现在来...
  • Pandas迭代

    2019-06-26 14:08:07
    其他数据结构,如:DataFrame和Panel,遵循类似惯例迭代对象的键。 简而言之,基本迭代(对于i在对象中)产生 - Series - 值 DataFrame - 列标签 Pannel - 项目标签 迭代DataFrame 迭代DataFrame提供列名。现在来...

空空如也

空空如也

1 2 3 4 5 ... 17
收藏数 326
精华内容 130
关键字:

dataframe迭代