pandas.DataFrame迭代

文章目录

  • 1. df.iteritems()
  • 2. df.iterrows()
  • 3. df.itertuples()
  • 4. 直接迭代
  • 5. columns, index, values迭代

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']

你可能感兴趣的:(python.pandas,进阶)