如何在Pandas中将Column-B wrt的唯一值分组并打印到Column-A?

时间:2016-01-13 00:55:15

标签: python pandas

示例数据:

tz = ["us","eu","ca","de","us","eu","ca","us","eu"]
ip = ["2.3.3.1","2.3.3.1","2.3.3.1","2.3.3.1","2.3.3.1","5.3.3.1","2.3.3.1","4.3.3.1","2.3.3.1"]
val = ["2.3","2.3","2.3","2.3","2.3","2.3","2.3","2.3","2.3"]
r = pd.DataFrame({'tz':tz,'ip':ip,'val':val})
r
r.groupby(tz).describe()

我希望将结果打印为:

ca --> 2.3.3.1
de --> 2.3.3.1
eu --> 2.3.3.1, 5.3.3.1 
us --> 2.3.3.1, 4.3.3.1

2 个答案:

答案 0 :(得分:2)

unique有效:

>>> u = r.groupby(tz)['ip'].unique()
>>> for i in u.index:
        print(i, '-->', ', '.join(u[i]))

ca --> 2.3.3.1
de --> 2.3.3.1
eu --> 2.3.3.1, 5.3.3.1
us --> 2.3.3.1, 4.3.3.1

答案 1 :(得分:0)

timezones = r['tz'].unique()

for timezone in timezones:
    print timezone + "  --->  " + ", ".join([i for i in list(r[r['tz']==timezone]['ip'].unique()])

这将按照要求将其打印出来......虽然应该直接对其进行调整以捕获列表等。