复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47'''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'>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23'''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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12'''除了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']))
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62'''以下几种写法等价''' 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
复制代码
1
2'''最好不用for,用apply比较好'''
最后
以上就是无情煎饼最近收集整理的关于iterrows和enumerate和for循环的全部内容,更多相关iterrows和enumerate和for循环内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复