1. 程式人生 > >iterrows和enumerate和for迴圈

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比較好'''