在列中创建分组元素列表(pandas)

时间:2018-02-07 12:29:35

标签: python list pandas

数据:

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

我需要通过以这种方式将id_num与pandas

分组来创建列表

id_list =(6589,8956,1064)

1 个答案:

答案 0 :(得分:1)

您似乎需要uniquedrop_duplicates并转换为tuple

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

print (a)
(6589, 8956, 1064)

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

如果想要list

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

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