概述
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比较好'''
最后
以上就是无情煎饼为你收集整理的iterrows和enumerate和for循环的全部内容,希望文章能够帮你解决iterrows和enumerate和for循环所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复