gpt4 book ai didi

python - 如何在 pandas 中将 B 列的唯一值分组并打印到 A 列?

转载 作者:行者123 更新时间:2023-12-01 04:12:22 24 4
gpt4 key购买 nike

示例数据:

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

最佳答案

独特作品:

>>> 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

关于python - 如何在 pandas 中将 B 列的唯一值分组并打印到 A 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34756627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com