Annalix
Annalix

Reputation: 480

Create a list of grouped element in a column (pandas)

Data:

id_num  House Color
6589    Smith yellow
6589    Cordero pink 
6589    Martinez brown
6589    Rossi  black
8956    Portella green
8956    Fusco  purple
8956    Benito white
1064    Dimingo  red
1064    Martinez indigo
1064    Schultz  violet

I need to create a list by grouping the id_num in this way with pandas

id_list = (6589,8956,1064)

Upvotes: 0

Views: 44

Answers (1)

jezrael
jezrael

Reputation: 863216

It seems you need unique or drop_duplicates and convert to tuple:

a = tuple(df.id_num.unique())

a = tuple(df.id_num.drop_duplicates())

print (a)
(6589, 8956, 1064)

print (type(a))
<class 'tuple'>

If want list:

b = df.id_num.unique().tolist()

b = df.id_num.drop_duplicates().tolist()
print (b)
[6589, 8956, 1064]

print (type(b))
<class 'list'>

Upvotes: 1

Related Questions