《python-for-data-analysis》中提到利用enumerate遍历,我联想到iterrows对dataframe的遍历,以及对字典的遍历,故有此记录。
import numpy as np
import pandas as pd
alist=["a","b","c","d"]
#enumerate
for index,value in enumerate(alist):
print(index,end="")
print(value)
#0a
#1b
#2c
#3d
df=pd.DataFrame(np.arange(10).reshape(2,5),index=list("ab"),columns=list("ABCDE"))
print(df)
for index,row in df.iterrows():
print(index)
print(row)
"""
A B C D E
a 0 1 2 3 4
b 5 6 7 8 9
a
A 0
B 1
C 2
D 3
E 4
Name: a, dtype: int32
b
A 5
B 6
C 7
D 8
E 9
Name: b, dtype: int32
"""
a_dict=(df.T).set_index("a").to_dict()["b"]
print(a_dict)
for key,value in a_dict.items():
print(key,end="")
print(value)
"""
{0: 5, 1: 6, 2: 7, 3: 8, 4: 9}
05
16
27
38
49
"""
以上在,记录本人学习过程。