3 个有意思的函数分别是zip()、cut()、enumerate()。
zip() 将元素打包成元祖组,然后返回可支持遍历的对象
1 2 3 4 5 6
| a = [1,2,3] b = [4,5,6] c = [4,5,6,7,8] for i in zip(a, b): print(i)
|
1 2 3
| for x in zip(a,b, c): print(x)
|
1 2 3
| (1, 4, 4) (2, 5, 5) (3, 6, 6)
|
PandasData数据类型的cut()函数,将数据分组打标签
1 2 3 4 5 6 7
| import pandas as pd import numpy as np
df = pd.DataFrame({ 'A': np.arange(20,30) }) df
|
1 2
| pd.cut(df['A'], bins=[10,20,25,30], right=True)
|
1 2 3 4 5 6 7 8 9 10 11 12
| 0 (10, 20] 1 (20, 25] 2 (20, 25] 3 (20, 25] 4 (20, 25] 5 (20, 25] 6 (25, 30] 7 (25, 30] 8 (25, 30] 9 (25, 30] Name: A, dtype: category Categories (3, interval[int64]): [(10, 20] < (20, 25] < (25, 30]]
|
1
| pd.cut(df['A'], bins=[10,20,25,30], right=False)
|
1 2 3 4 5 6 7 8 9 10 11 12
| 0 [20, 25) 1 [20, 25) 2 [20, 25) 3 [20, 25) 4 [20, 25) 5 [25, 30) 6 [25, 30) 7 [25, 30) 8 [25, 30) 9 [25, 30) Name: A, dtype: category Categories (3, interval[int64]): [[10, 20) < [20, 25) < [25, 30)]
|
1 2
| pd.cut(df['A'], bins=[10,20,25,30], labels=['低劣','普通','良好'])
|
1 2 3 4 5 6 7 8 9 10 11 12
| 0 低劣 1 普通 2 普通 3 普通 4 普通 5 普通 6 良好 7 良好 8 良好 9 良好 Name: A, dtype: category Categories (3, object): [低劣 < 普通 < 良好]
|
enumerate()函数获取迭代数据序号
1 2 3 4 5 6
| seasons = ['Spring', 'Summer', 'Fall', 'Winter'] print(list(enumerate(seasons)))
print(list(enumerate(seasons, start=1)))
|
1 2
| [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
|
1 2 3
| seq = ['one', 'two', 'three'] for i, element in enumerate(seq): print(i, element)
|