python change dataframe column into set and loop the set

description: change dataframe column into set and iterate

change dataframe column into a set

1
2
3
4
5
6
7
8
9
10
11
12
import pandas
a = pandas.DataFrame([[1,2],[2,3],[4,5]], columns=['a','b'])
# a
# a b
# 0 1 2
# 1 2 3
b = a['a'].unique()
# b
# array([1, 2, 4], dtype=int64)
b = set(b)
# b
# {1, 2, 4}

iterate dataframe column set

1
2
3
4
5
for item in b:
print(item)
# 1
# 2
# 4

combine into a single code

1
2
3
4
5
for item in a['a'].unique():
print(item)
# 1
# 2
# 4

more on search4fan.github.io